Created
January 3, 2018 20:29
-
-
Save maticzav/9ffad96e78ee65111043c233f6c43587 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// src/resolvers/Mutation/auth.ts | |
import * as jwt from 'jsonwebtoken' | |
export const auth = { | |
authenticate: async (parent, { githubCode }, ctx: Context, info) => { | |
// ... | |
return { | |
token: jwt.sign({ userId: user.id }, process.env.JWT_SECRET), // <- Sign in | |
user | |
} | |
} | |
} | |
// src/utils.ts | |
import * as jwt from 'jsonwebtoken' | |
export function getUserId(context) { | |
const Authorization = context.request.get('Authorization') | |
if (Authorization) { | |
const token = Authorization.replace('Bearer ', '') | |
const { userId } = jwt.verify(token, process.env.JWT_SECRET!) as { // <- Verification | |
userId: string | |
} | |
return userId | |
} | |
throw new AuthError() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment