generated from notiz-dev/nestjs-prisma-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.resolver.ts
50 lines (44 loc) · 1.22 KB
/
auth.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
import { Auth } from '../../models/auth.model';
import { Token } from '../../models/token.model';
import { LoginInput } from './dto/#.input';
import {
Resolver,
Mutation,
Args,
Parent,
ResolveField,
} from '@nestjs/graphql';
import { AuthService } from '../../services/auth.service';
import { #Input } from './dto/#.input';
@Resolver((of) => Auth)
export class AuthResolver {
constructor(private readonly auth: AuthService) {}
@Mutation((returns) => Auth)
async #(@Args('data') data: #Input) {
data.email = data.email.toLowerCase();
const { accessToken, refreshToken } = await this.auth.createUser(data);
return {
accessToken,
refreshToken,
};
}
@Mutation((returns) => Auth)
async login(@Args('data') { email, password }: LoginInput) {
const { accessToken, refreshToken } = await this.auth.login(
email.toLowerCase(),
password
);
return {
accessToken,
refreshToken,
};
}
@Mutation((returns) => Token)
async refreshToken(@Args('token') token: string) {
return this.auth.refreshToken(token);
}
@ResolveField('user')
async user(@Parent() auth: Auth) {
return await this.auth.getUserFromToken(auth.accessToken);
}
}