-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nkshah2/supertokens-integration
Supertokens integration
- Loading branch information
Showing
8 changed files
with
679 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
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
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); | ||
}); | ||
}) | ||
} |
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
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
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
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; | ||
} | ||
}, | ||
}; | ||
} |
Oops, something went wrong.