Skip to content

Instantly share code, notes, and snippets.

@putrikarunia
putrikarunia / middleware.go
Last active December 29, 2020 21:42
Go cotter auth middleware
package middleware
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
@putrikarunia
putrikarunia / auth_hasura.js
Created November 25, 2020 21:37
Check requests Authorization header, and return Hasura headers
// ===========================================================================
// Check if the Authorization header contains a valid Cotter Access Token
// then return Hasura headers for authentication
// ===========================================================================
// REQUIREMENTS:
// - API_KEY_ID env variables using Cotter API keys
async function handleWithCorsHeader(request) {
const resp = await handleRequest(request);
// ======================================================
// Refresh Google's Access Token when Expired
// ======================================================
// Access tokens from Google can expire, use this function to refresh the access token
// the `refresh_token` can be retrieved from Cotter's API
// REQUIREMENTS:
// - GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET env variables using Google OAuth 2.0 Credentials
async function refreshGoogleToken(refresh_token) {
// ======================================================
// Handle Options to pass CORS
// ======================================================
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, HEAD, POST, PUT, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, API_KEY_ID, Authorization',
'Access-Control-Max-Age': '86400',
}
// ======================================================
// Validating the JWT Token
// ======================================================
// USAGE
// const resp = await validateJWT(access_token, apiKeyID)
//
// resp = { valid: true }
// or
// resp = { valid: false, reason: "error message" }
<!-- 2. Initialize Cotter -->
<script>
var cotter = new Cotter({
ApiKeyID: "<YOUR_API_KEY_ID>", // 👈 Specify your API KEY ID here
Styles: {
input_label: {
color: "red", // 👈 Specify your color here, use HEX, like #FFFFFF
},
},
});
import Cotter from "cotter";
export default function Dashboard() {
...
// Get a list of repositories
useEffect(() => {...}, []);
const getRepositories = async () => {...};
const connectToGithub = async () => {
import Navbar from "../components/Navbar";
export default function Dashboard() {
...
return (
<>
<Navbar /> // Add the navbar
<div className={styles.container}>...</div>
</>
);
import Navbar from "../components/Navbar";
export default function Home() {
...
return (
<>
<Navbar /> // Add the navbar
<div className={styles.container}>...</div>
</>
);
const logOut = async () => {
const cotter = new Cotter(API_KEY_ID); // 👈 Specify your API KEY ID here
await cotter.logOut();
setloggedIn(false);
window.location.href = "/";
};