Skip to content

Commit

Permalink
Merge pull request #1 from nkshah2/supertokens-integration
Browse files Browse the repository at this point in the history
Supertokens integration
  • Loading branch information
rishabhpoddar authored Dec 22, 2021
2 parents 9c11a5d + 07c5797 commit 5054e00
Show file tree
Hide file tree
Showing 8 changed files with 679 additions and 22 deletions.
10 changes: 9 additions & 1 deletion packages/api-server/src/requestHandlers/awsLambdaFastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@ const fastifyResponseForLambdaResult = (
reply: FastifyReply,
lambdaResult: APIGatewayProxyResult
) => {
const { statusCode = 200, headers, body = '' } = lambdaResult
const { statusCode = 200, headers, body = '', multiValueHeaders } = lambdaResult

if (headers) {
Object.keys(headers).forEach((headerName) => {
const headerValue: any = headers[headerName]
reply.header(headerName, headerValue)
})
}

if (multiValueHeaders) {
Object.keys(multiValueHeaders).forEach((headerName) => {
const headerValue: Array<any> = multiValueHeaders[headerName]
reply.header(headerName, headerValue)
})
}

reply.status(statusCode)

if (lambdaResult.isBase64Encoded) {
Expand Down
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/md5": "2.3.1",
"aws-lambda": "1.0.7",
"jest": "27.4.5",
"supertokens-node": "^8.3.0",
"typescript": "4.5.4"
},
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/auth/decoders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { magicLink } from './magicLink'
import { netlify } from './netlify'
import { nhost } from './nhost'
import { supabase } from './supabase'
import {supertokens} from './supertokens';

interface Req {
event: APIGatewayProxyEvent
Expand All @@ -37,6 +38,7 @@ const typesToDecoders: Record<
supabase: supabase,
ethereum: ethereum,
dbAuth: dbAuth,
supertokens: supertokens,
custom: custom,
}

Expand Down
33 changes: 33 additions & 0 deletions packages/api/src/auth/decoders/supertokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import jwt from 'jsonwebtoken'
import jwksClient from 'jwks-rsa'

export const supertokens = async (token: string): Promise<any | null> => {
return new Promise((resolve, reject) => {
const {SUPERTOKENS_JWKS_URL} = process.env;

if (SUPERTOKENS_JWKS_URL === undefined) {
return reject(new Error("SUPERTOKENS_JWKS_URL environment variable is not set"));
}

const client = jwksClient({
jwksUri: SUPERTOKENS_JWKS_URL,
});

function getKey(header: any, callback: jwt.SigningKeyCallback){
client.getSigningKey(header.kid, function(err: any, key: any) {
var signingKey = key.publicKey || key.rsaPublicKey;
callback(err, signingKey);
});
}

jwt.verify(token, getKey, {}, function(err, decoded) {
if (err) {
return reject(err);
}

decoded = decoded === null ? {} : decoded;

return resolve(decoded);
});
})
}
1 change: 1 addition & 0 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"netlify-identity-widget": "1.9.2",
"nhost-js-sdk": "3.1.0",
"react": "17.0.2",
"supertokens-auth-react": "^0.17.7",
"typescript": "4.5.4"
},
"repository": {
Expand Down
5 changes: 5 additions & 0 deletions packages/auth/src/authClients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { nhost } from './nhost'
import type { Nhost, NhostUser } from './nhost'
import { supabase } from './supabase'
import type { Supabase, SupabaseUser } from './supabase'
import {supertokens} from './supertokens';
import type { SuperTokensUser, SuperTokens} from './supertokens';

const typesToClients = {
netlify,
Expand All @@ -38,6 +40,7 @@ const typesToClients = {
ethereum,
nhost,
clerk,
supertokens,
/** Don't we support your auth client? No problem, define your own the `custom` type! */
custom,
}
Expand All @@ -54,6 +57,7 @@ export type SupportedAuthClients =
| Clerk
| Ethereum
| Nhost
| SuperTokens
| Custom

export type SupportedAuthTypes = keyof typeof typesToClients
Expand All @@ -78,6 +82,7 @@ export type SupportedUserMetadata =
| SupabaseUser
| EthereumUser
| NhostUser
| SuperTokensUser

export interface AuthClient {
restoreAuthState?(): void | Promise<any>
Expand Down
45 changes: 45 additions & 0 deletions packages/auth/src/authClients/supertokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Sessions from "supertokens-auth-react/recipe/session";

import type { AuthClient } from './';

export interface SuperTokensUser {
userId: string;
accessTokenPayload: any;
}

export type SuperTokens = AuthClient;

export const supertokens = (client: {
authRecipe: any,
}): AuthClient => {
return {
type: "supertokens",
client: undefined,
login: async () => client.authRecipe.redirectToAuth('signin'),

signup: async () => client.authRecipe.redirectToAuth('signup'),

logout: async () => Sessions.signOut(),

getToken: async (): Promise<string | null> => {
if (await Sessions.doesSessionExist()) {
let accessTokenPayload = await Sessions.getAccessTokenPayloadSecurely();
let jwtPropertyName = accessTokenPayload["_jwtPName"];
return accessTokenPayload[jwtPropertyName];
} else {
return null;
}
},

getUserMetadata: async (): Promise<SuperTokensUser | null> => {
if (await Sessions.doesSessionExist()) {
return {
userId: await Sessions.getUserId(),
accessTokenPayload: await Sessions.getAccessTokenPayloadSecurely(),
}
} else {
return null;
}
},
};
}
Loading

0 comments on commit 5054e00

Please sign in to comment.