This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
user.resolver.ts
101 lines (90 loc) · 2.93 KB
/
user.resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { Resolver, Query, Ctx, Mutation, Arg, Authorized } from "type-graphql"
import { ResolverContext } from "../../lib/types"
import { createToken, decryptToken } from "../../lib/jwt"
import { User } from "./user.entity"
import { UserService } from "./user.service"
import { UserMailer } from "./user.mailer"
import { RegisterInput } from "./inputs/register.input"
import { LoginInput } from "./inputs/login.input"
import { UpdateInput } from "./inputs/update.input"
import { ResetPasswordInput } from "./inputs/resetPassword.input"
import { UserRepository } from "./user.repository"
import { CurrentUser } from "../shared/middleware/currentUser"
import { SlackService } from "../shared/slack.service"
@Resolver(() => User)
export class UserResolver {
constructor(
private readonly userService: UserService,
private readonly userRepository: UserRepository,
private readonly userMailer: UserMailer,
private readonly slackService: SlackService,
) {}
// ME
@Authorized()
@Query(() => User, { nullable: true })
me(@CurrentUser() currentUser: User): Promise<User> {
return this.userRepository.findById(currentUser.id)
}
// REGISTER
@Mutation(() => User)
async register(
@Arg("data") data: RegisterInput,
@Ctx() { req }: ResolverContext,
): Promise<User> {
const user = await this.userService.create(data)
if (req.session) req.session.user = user
this.userMailer.sendWelcomeEmail(user)
this.slackService.sendChatMessage(
`${user.firstName} ${user.lastName} signed up!`,
)
return user
}
// LOGIN
@Mutation(() => User)
async login(
@Arg("data") data: LoginInput,
@Ctx() { req }: ResolverContext,
): Promise<User> {
const user = await this.userService.login(data)
if (req.session) req.session.user = user
return user
}
// UPDATE USER
@Authorized()
@Mutation(() => User, { nullable: true })
async updateUser(
@Arg("data") data: UpdateInput,
@CurrentUser() currentUser: User,
): Promise<User> {
return this.userService.update(currentUser.id, data)
}
// LOGOUT
@Mutation(() => Boolean)
async logout(@Ctx() { req, res }: ResolverContext): Promise<boolean> {
await new Promise(res => {
if (req.session) req.session.destroy(() => res())
})
res.clearCookie("split.cookie")
return true
}
// FORGOT PASSWORD
@Mutation(() => Boolean)
async forgotPassword(@Arg("email") email: string): Promise<boolean> {
const user = await this.userRepository.findByEmail(email)
if (!user) throw new Error("User not found")
const token = await createToken(user.id)
this.userMailer.sendResetPasswordLink(user, token)
return true
}
// RESET PASSWORD
@Mutation(() => Boolean)
async resetPassword(@Arg("data")
{
token,
password,
}: ResetPasswordInput): Promise<boolean> {
const payload: { id: string } = await decryptToken(token)
await this.userService.update(payload.id, { password })
return true
}
}