Skip to content

Instantly share code, notes, and snippets.

@maticzav
Created January 3, 2018 20:29
Show Gist options
  • Save maticzav/9ffad96e78ee65111043c233f6c43587 to your computer and use it in GitHub Desktop.
Save maticzav/9ffad96e78ee65111043c233f6c43587 to your computer and use it in GitHub Desktop.
// 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