You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
demo-graphql-oauth/src/server/UserResolver.ts

56 lines
1.5 KiB

import "reflect-metadata"
import { Arg, Authorized, Ctx, Mutation, Query } from "type-graphql"
import {
accessTokenWithRefreshCookie,
comparePasswords,
Context,
createRtCookie,
} from "./UserResolver/auth"
import { User } from "./UserResolver/User"
export class UserResolver {
// TODO: remove when other query gets itroduced
@Query(() => String)
async query() {
return ""
}
@Mutation(() => User)
async createUser(@Arg("email") email: string, @Arg("password") password: string) {
return await User.create({
email,
password,
}).save()
}
@Mutation(() => String)
async accessToken(
@Arg("email") email: string,
@Arg("password") password: string,
@Ctx() { res }: Context
) {
try {
const user = await User.findOne({ where: { email } })
await comparePasswords(user!.password, password)
return accessTokenWithRefreshCookie(user!.id, user!.tokenVersion, res)
} catch (error) {
throw new Error("Login credentials are invalid: " + error)
}
}
@Mutation(() => User)
@Authorized()
async me(@Ctx() { payload }: Context) {
return await User.findOne({
where: { id: payload!.uid },
})
}
@Mutation(() => Boolean)
async signOut(@Ctx() { res }: Context) {
createRtCookie(res, "")
return true
}
}