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.
45 lines
1.1 KiB
45 lines
1.1 KiB
import "reflect-metadata"
|
|
import { Arg, Mutation, Query, Resolver } from "type-graphql"
|
|
import * as argon2 from "../../utils/argon2"
|
|
import * as jwt from "../../utils/jwt"
|
|
import { User } from "../User"
|
|
|
|
@Resolver(() => User)
|
|
export class UserResolver {
|
|
@Query(() => [User])
|
|
async users() {
|
|
return await User.find()
|
|
}
|
|
|
|
@Query(() => String, { nullable: true })
|
|
async loginToken(
|
|
@Arg("email") email: string,
|
|
@Arg("password") password: string
|
|
): Promise<string | null> {
|
|
const user = await User.findOne({ where: { email } })
|
|
|
|
if (!user) {
|
|
return null
|
|
}
|
|
|
|
const passwordValid = await argon2.verify(user.password, password)
|
|
|
|
if (!passwordValid) {
|
|
return null
|
|
}
|
|
|
|
const token = jwt.signWithRS256({ userId: user.id })
|
|
return token
|
|
}
|
|
|
|
@Mutation(() => User)
|
|
async createUser(
|
|
@Arg("email") email: string,
|
|
@Arg("password") password: string
|
|
): Promise<User> {
|
|
return await User.create({
|
|
email,
|
|
password,
|
|
}).save()
|
|
}
|
|
}
|
|
|