File size: 2,776 Bytes
7b850b7 |
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 102 103 104 105 106 107 108 109 110 111 112 113 114 |
import {
Body,
Controller,
Get,
HttpStatus,
Inject,
Param,
Post,
Put,
Query,
Req,
Res,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { Response, Request } from 'express';
import { AuthService } from './auth.service';
import * as bcrypt from 'bcrypt';
import { LocalAuthGuard } from './local-auth.guard';
import { UserService } from '../user/user.service';
import { JwtAuthGuard } from './jwt-auth.guard';
import * as jwt from 'jsonwebtoken';
import { FileInterceptor } from '@nestjs/platform-express';
import { Model } from 'mongoose';
import { AuthGuard } from '@nestjs/passport';
import { JwtService } from '@nestjs/jwt';
import { CommonServices } from '../shared/common.service';
import { jwtConstants } from 'src/constants/jwt.constant';
@Controller('auth')
export class AuthController extends CommonServices {
constructor(
private readonly authService: AuthService,
private readonly userService: UserService,
) {
super();
}
@UseGuards(LocalAuthGuard)
@Post('/login')
async login(
@Req() req: any,
@Res() res: Response,
@Body() body: any,
): Promise<any> {
try {
const Userresp: any = await this.authService.login(req.user, body);
return this.sendResponse(
this.messages.Success,
Userresp,
HttpStatus.OK,
res,
);
} catch (error) {
console.log(`error =>`, error);
return this.sendResponse(
'Internal server Error',
{},
HttpStatus.INTERNAL_SERVER_ERROR,
res,
);
}
}
@Post('/signup')
async signup(
@Req() req: any,
@Res() res: Response,
@Body() body: any,
): Promise<any> {
try {
const email = body.email.toLowerCase().trim();
const checkUser = await this.userService.sharedFindOne({ email: email });
if (checkUser && checkUser.email == email) {
return this.sendResponse(
this.messages.userAlreadyExist,
{},
HttpStatus.CONFLICT,
res,
);
}
const createUser = await this.userService.sharedCreate({
...body,
password: bcrypt.hashSync(body.password, jwtConstants.salt),
roles: ['user'],
});
const Userresp: any = await this.authService.login(createUser);
return this.sendResponse(
'Account created!',
{
...Userresp,
},
HttpStatus.OK,
res,
);
} catch (error) {
console.log(error);
return this.sendResponse(
'Internal server Error',
{},
HttpStatus.INTERNAL_SERVER_ERROR,
res,
);
}
}
}
|