Login Endpoint
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { AppController } from './app.controller';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule],
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
expandVariables: true,
|
||||
}),
|
||||
AuthModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [],
|
||||
})
|
||||
|
||||
8
backend/src/auth/login/dto/kc.data.dto.ts
Normal file
8
backend/src/auth/login/dto/kc.data.dto.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export class KeycloakDataDto {
|
||||
client_id: string;
|
||||
client_secret: string;
|
||||
grant_type: string;
|
||||
username?: string;
|
||||
email?: string;
|
||||
password: string;
|
||||
}
|
||||
@@ -1 +1,5 @@
|
||||
export class LoginDto {}
|
||||
export class LoginDto {
|
||||
username?: string;
|
||||
email?: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
10
backend/src/auth/login/dto/token.dto.ts
Normal file
10
backend/src/auth/login/dto/token.dto.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export class TokenDto {
|
||||
access_token: string;
|
||||
expires_in: number;
|
||||
refresh_token: string;
|
||||
refresh_expires_in: number;
|
||||
token_type: string;
|
||||
'not-before-policy': number;
|
||||
session_state: string;
|
||||
scope: string;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller, Post, Body } from '@nestjs/common';
|
||||
import { Controller, Post, Body, Get } from '@nestjs/common';
|
||||
import { LoginService } from './login.service';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { LoginService } from './login.service';
|
||||
import { LoginController } from './login.controller';
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
|
||||
@Module({
|
||||
imports: [HttpModule],
|
||||
controllers: [LoginController],
|
||||
providers: [LoginService]
|
||||
providers: [LoginService],
|
||||
})
|
||||
export class LoginModule {}
|
||||
|
||||
@@ -1,9 +1,40 @@
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { lastValueFrom } from 'rxjs';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
import { KeycloakDataDto } from './dto/kc.data.dto';
|
||||
const querystring = require('querystring');
|
||||
|
||||
@Injectable()
|
||||
export class LoginService {
|
||||
login(loginDto: LoginDto) {
|
||||
return 'This action adds a new login';
|
||||
constructor(private readonly httpService: HttpService) {}
|
||||
|
||||
async login(loginDto: LoginDto) {
|
||||
const keycloakHeaders = {
|
||||
Accept: 'application/xhtml+xml',
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
};
|
||||
|
||||
const keycloakTokenData = {
|
||||
client_id: process.env.KC_CLIENT_ID || 'client_id',
|
||||
client_secret: process.env.KC_CLIENT_SECRET || 'client_secret',
|
||||
grant_type: process.env.KC_GRANT_TYPE || 'grant_type',
|
||||
};
|
||||
|
||||
const kcData: KeycloakDataDto = { ...keycloakTokenData, ...loginDto };
|
||||
|
||||
try {
|
||||
const res = await lastValueFrom(
|
||||
this.httpService.request({
|
||||
method: 'POST',
|
||||
data: querystring.stringify(kcData),
|
||||
headers: keycloakHeaders,
|
||||
url: process.env.KC_TOKEN_ENDPOINT,
|
||||
}),
|
||||
);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { RegisterService } from './register.service';
|
||||
import { RegisterController } from './register.controller';
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
|
||||
@Module({
|
||||
imports: [HttpModule],
|
||||
controllers: [RegisterController],
|
||||
providers: [RegisterService]
|
||||
})
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AxiosResponse } from 'axios';
|
||||
import { lastValueFrom, Observable, of } from 'rxjs';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
|
||||
@Injectable()
|
||||
export class RegisterService {
|
||||
constructor(private readonly httpService: HttpService) {}
|
||||
|
||||
create(createRegisterDto: CreateUserDto) {
|
||||
return 'This action adds a new register';
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all register`;
|
||||
async findAll() {
|
||||
// const obj = await lastValueFrom(this.httpService.get('https://jsonplaceholder.typicode.com/todos'))
|
||||
// return obj.data;
|
||||
return 'Find all Users';
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
|
||||
@@ -3,6 +3,6 @@ import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
await app.listen(process.env.APP_PORT || '3000');
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
Reference in New Issue
Block a user