This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package middleware | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"strings" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// =========================================================================== | |
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ====================================================== | |
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ====================================================== | |
// 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', | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ====================================================== | |
// Validating the JWT Token | |
// ====================================================== | |
// USAGE | |
// const resp = await validateJWT(access_token, apiKeyID) | |
// | |
// resp = { valid: true } | |
// or | |
// resp = { valid: false, reason: "error message" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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 | |
}, | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const logOut = async () => { | |
const cotter = new Cotter(API_KEY_ID); // 👈 Specify your API KEY ID here | |
await cotter.logOut(); | |
setloggedIn(false); | |
window.location.href = "/"; | |
}; |
NewerOlder