1- import { mutationField , stringArg , objectType } from " yoga" ;
1+ import { mutationField , stringArg , objectType } from ' yoga'
22import * as jwt from 'jsonwebtoken'
3- import { getGithubToken , getGithubUser , GithubUser } from '../github' ;
4- import { Context } from "../context" ;
5- import { User } from "../../.yoga/prisma-client" ;
6- import config from "../config" ;
7- import { PayloadInterface } from "./PayloadInterface" ;
8- import { AuthorizeResolver } from "nexus/dist/core" ;
9- import { NexusGenRootTypes } from "../../.yoga/nexus" ;
3+ import { getGithubToken , getGithubUser , GithubUser } from '../github'
4+ import { Context } from '../context'
5+ import { User } from '../../.yoga/prisma-client'
6+ import config from '../config'
7+ import { PayloadInterface } from './PayloadInterface'
8+ import { AuthorizeResolver } from 'nexus/dist/core'
109
1110export const AuthenticateUserPayload = objectType ( {
12- name : " AuthenticateUserPayload" ,
13- definition : ( t ) => {
14- t . implements ( PayloadInterface ) ;
15- t . field ( " user" , {
16- type : " User"
17- } )
18- t . string ( " token" )
19- }
11+ name : ' AuthenticateUserPayload' ,
12+ definition : t => {
13+ t . implements ( PayloadInterface )
14+ t . field ( ' user' , {
15+ type : ' User' ,
16+ } )
17+ t . string ( ' token' )
18+ } ,
2019} )
2120
22- export const authenticate = mutationField ( " authenticate" , {
23- type : AuthenticateUserPayload ,
24- nullable : true ,
25- args : {
26- githubCode : stringArg ( {
27- required : true ,
28- description : " GitHub OAuth Token from the client."
29- } )
30- } ,
31- resolve : async ( _ , { githubCode } , ctx ) => {
32- const githubToken = await getGithubToken ( githubCode )
33- const githubUser = await getGithubUser ( githubToken )
34- let user = await getPrismaUser ( ctx , githubUser . id . toString ( ) )
21+ export const authenticate = mutationField ( ' authenticate' , {
22+ type : AuthenticateUserPayload ,
23+ nullable : true ,
24+ args : {
25+ githubCode : stringArg ( {
26+ required : true ,
27+ description : ' GitHub OAuth Token from the client.' ,
28+ } ) ,
29+ } ,
30+ resolve : async ( _ , { githubCode } , ctx ) => {
31+ const githubToken = await getGithubToken ( githubCode )
32+ const githubUser = await getGithubUser ( githubToken )
33+ let user = await getPrismaUser ( ctx , githubUser . id . toString ( ) )
3534
36- if ( ! user ) {
37- user = await createPrismaUser ( ctx , githubUser )
38- }
35+ if ( ! user ) {
36+ user = await createPrismaUser ( ctx , githubUser )
37+ }
3938
40- return {
41- success : true ,
42- message : null ,
43- code : null ,
44- token : jwt . sign ( { userId : user . id } , config . jwt . SECRET ) ,
45- user
46- }
39+ return {
40+ success : true ,
41+ message : null ,
42+ code : null ,
43+ token : jwt . sign ( { userId : user . id } , config . jwt . SECRET ) ,
44+ user,
4745 }
46+ } ,
4847} )
4948
5049// Helpers -------------------------------------------------------------------
5150
52- async function getPrismaUser ( ctx : Context , githubUserId : string ) : Promise < User > {
53- return await ctx . prisma . user ( { githubUserId } )
51+ async function getPrismaUser (
52+ ctx : Context ,
53+ githubUserId : string ,
54+ ) : Promise < User > {
55+ return await ctx . prisma . user ( { githubUserId } )
5456}
5557
56- async function createPrismaUser ( ctx : Context , githubUser : GithubUser ) : Promise < User > {
57- const user = await ctx . prisma . createUser ( {
58- githubUserId : githubUser . id . toString ( ) ,
59- name : githubUser . name ,
60- email : githubUser . email ,
61- githubHandle : githubUser . login ,
62- bio : githubUser . bio ,
63- avatarUrl : githubUser . avatar_url
64- } )
65- return user
58+ async function createPrismaUser (
59+ ctx : Context ,
60+ githubUser : GithubUser ,
61+ ) : Promise < User > {
62+ const user = await ctx . prisma . createUser ( {
63+ githubUserId : githubUser . id . toString ( ) ,
64+ name : githubUser . name ,
65+ email : githubUser . email ,
66+ githubHandle : githubUser . login ,
67+ bio : githubUser . bio ,
68+ avatarUrl : githubUser . avatar_url ,
69+ } )
70+ return user
6671}
6772
68- export const authorizeUser = ( ) : AuthorizeResolver < any , any > => ( parent , args , ctx ) => {
69- if ( ctx . currentUserId ) {
70- return true ;
71- }
72- return false ;
73- } ;
73+ export const authorizeUser = ( ) : AuthorizeResolver < any , any > => (
74+ parent ,
75+ args ,
76+ ctx ,
77+ ) => {
78+ if ( ctx . currentUserId ) {
79+ return true
80+ }
81+ return false
82+ }
0 commit comments