-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.resolver.ts
90 lines (79 loc) · 2.47 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
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
import { Auth } from '../../models/auth.model';
import { Token } from '../../models/token.model';
import { LoginInput } from './dto/login.input';
import {
Resolver,
Mutation,
Args,
Parent,
ResolveField,
Context,
} from '@nestjs/graphql';
import { AuthService } from '../../services/auth.service';
import { SignupInput } from './dto/signup.input';
import { User } from 'src/models/user.model';
import { GqlAuthGuard } from 'src/guards/gql-auth.guard';
import { UseGuards, Headers, Req, Request, Response } from '@nestjs/common';
import { GqlAuthResponseStatus } from 'src/models/auth-response-status-gql.model';
import { UserEntity } from 'src/decorators/user.decorator';
import { ResponseBoolean } from 'src/models/responseBoolean';
@Resolver((of) => Auth)
export class AuthResolver {
constructor(private readonly auth: AuthService) { }
@Mutation((returns) => Auth)
async signup(@Args('data') data: SignupInput) {
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) => ResponseBoolean)
async changeForgotPassword(@Args('email') email: string) {
const data = await this.auth.forgotPassword(email)
return {
status: data,
};
}
@Mutation((returns) => Token)
async changePasswordwithEmail(@Args('token') token: string, @Args('password') password: string) {
return await this.auth.changePassword(token, password);
}
@UseGuards(GqlAuthGuard)
@Mutation((returns) => GqlAuthResponseStatus)
async signOut(@Context('req') req) {
console.log(req.headers);
const tokenNew = req.headers.authorization.split(' ')[1];
if (this.auth.signOut(tokenNew)) {
return {
status: 'error',
};
} else {
return {
status: 'error',
};
}
// Basarisiz token dogrulamasinda hata firlatmasi gerekiyor
}
@Mutation((returns) => Token)
async refreshToken(@Args('token') token: string) {
return this.auth.refreshToken(token);
}
@UseGuards(GqlAuthGuard)
@ResolveField('user', (of) => User)
async user(@Parent() auth: Auth) {
return await this.auth.getUserFromToken(auth.accessToken);
}
}