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