parent
477787785e
commit
586efd5581
File diff suppressed because it is too large
Load Diff
@ -1,14 +1,45 @@ |
|||||||
import "reflect-metadata"; |
import "reflect-metadata" |
||||||
import { Query, Resolver } from "type-graphql"; |
import { Arg, Mutation, Query, Resolver } from "type-graphql" |
||||||
import { getRepository } from "typeorm"; |
import * as argon2 from "../../utils/argon2" |
||||||
import { User } from "../User"; |
import * as jwt from "../../utils/jwt" |
||||||
|
import { User } from "../User" |
||||||
|
|
||||||
@Resolver(_of => User) |
@Resolver(() => User) |
||||||
export class UserResolver { |
export class UserResolver { |
||||||
|
@Query(() => [User]) |
||||||
@Query(_returns => [User]) |
|
||||||
async users() { |
async users() { |
||||||
const userRepository = getRepository(User) |
return await User.find() |
||||||
return userRepository.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() |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -0,0 +1,10 @@ |
|||||||
|
import * as agron2 from "argon2" |
||||||
|
export * from "argon2" |
||||||
|
|
||||||
|
/** |
||||||
|
* Override the defaul agron2i option with agron2id |
||||||
|
* @param password Pasword to has using argon2id |
||||||
|
*/ |
||||||
|
export async function hashIncludingOptions(password: string) { |
||||||
|
return await agron2.hash(password, { type: agron2.argon2id }) |
||||||
|
} |
@ -1,7 +1,16 @@ |
|||||||
import { buildSchema } from "type-graphql"; |
import { buildSchema, MiddlewareFn } from "type-graphql" |
||||||
import { UserResolver } from "../modules/User/UserResolver"; |
import { UserResolver } from "../modules/User/UserResolver" |
||||||
|
|
||||||
|
const ErrorInterceptor: MiddlewareFn<any> = async ({}, next) => { |
||||||
|
try { |
||||||
|
return await next() |
||||||
|
} catch (err) { |
||||||
|
console.error(err) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
export const createSchema = () => |
export const createSchema = () => |
||||||
buildSchema({ |
buildSchema({ |
||||||
resolvers: [UserResolver], |
resolvers: [UserResolver], |
||||||
|
globalMiddlewares: [ErrorInterceptor], |
||||||
}) |
}) |
||||||
|
@ -0,0 +1,12 @@ |
|||||||
|
import * as jwt from "jsonwebtoken" |
||||||
|
export * from "jsonwebtoken" |
||||||
|
|
||||||
|
import fs = require('fs') |
||||||
|
import path = require('path') |
||||||
|
|
||||||
|
const PRIVATE_KEY = fs.readFileSync(path.join(__dirname, 'keys', 'jwtRS256.key')) |
||||||
|
export const PUBLIC_KEY = fs.readFileSync(path.join(__dirname, 'keys', 'jwtRS256.key.pub'))
|
||||||
|
|
||||||
|
export function signWithRS256(payload: string | object) { |
||||||
|
return jwt.sign(payload, PRIVATE_KEY, {algorithm: "RS256"}) |
||||||
|
} |
Loading…
Reference in new issue