switched to supertokens
This commit is contained in:
22
backend/src/app.controller.spec.ts
Normal file
22
backend/src/app.controller.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
// constructor() {}
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return 'Home';
|
||||
return this.appService.getHello();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { AppController } from './app.controller';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
expandVariables: true,
|
||||
}),
|
||||
AuthModule,
|
||||
],
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
8
backend/src/app.service.ts
Normal file
8
backend/src/app.service.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { LoginModule } from './login/login.module';
|
||||
import { RegisterModule } from './register/register.module';
|
||||
|
||||
@Module({
|
||||
imports: [LoginModule, RegisterModule],
|
||||
})
|
||||
export class AuthModule {}
|
||||
@@ -1,8 +0,0 @@
|
||||
export class KeycloakDataDto {
|
||||
client_id: string;
|
||||
client_secret: string;
|
||||
grant_type: string;
|
||||
username?: string;
|
||||
email?: string;
|
||||
password: string;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
export class LoginDto {
|
||||
username?: string;
|
||||
email?: string;
|
||||
password: string;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
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 +0,0 @@
|
||||
export class Login {}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Controller, Post, Body, Get } from '@nestjs/common';
|
||||
import { LoginService } from './login.service';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
|
||||
@Controller('login')
|
||||
export class LoginController {
|
||||
constructor(private readonly loginService: LoginService) {}
|
||||
|
||||
@Post()
|
||||
login(@Body() createLoginDto: LoginDto) {
|
||||
return this.loginService.login(createLoginDto);
|
||||
}
|
||||
|
||||
@Post()
|
||||
logout(@Body() createLoginDto: LoginDto) {
|
||||
return "Logout";
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { LoginService } from './login.service';
|
||||
import { LoginController } from './login.controller';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [LoginController],
|
||||
providers: [LoginService],
|
||||
})
|
||||
export class LoginModule {}
|
||||
@@ -1,37 +0,0 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
import { Issuer } from 'openid-client';
|
||||
|
||||
@Injectable()
|
||||
export class LoginService {
|
||||
async login(loginDto: LoginDto) {
|
||||
const { username, password } = loginDto;
|
||||
|
||||
const keycloakIssuer = await Issuer.discover(
|
||||
`${process.env.KC_BASE_URL}/auth/realms/${process.env.KC_REALM}`,
|
||||
);
|
||||
|
||||
const openIdConnectClient = new keycloakIssuer.Client({
|
||||
client_id: process.env.KC_CLIENT_ID || 'client_id',
|
||||
client_secret: process.env.KC_CLIENT_SECRET || 'client_secret',
|
||||
});
|
||||
|
||||
try {
|
||||
const token = await openIdConnectClient.grant({
|
||||
grant_type: process.env.KC_GRANT_TYPE || 'grant_type',
|
||||
username,
|
||||
password,
|
||||
});
|
||||
|
||||
return token;
|
||||
} catch (error) {
|
||||
throw new HttpException(error.error_description, HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// {
|
||||
// "error": "invalid_grant",
|
||||
// "error_description": "Invalid user credentials",
|
||||
// "name": "OPError"
|
||||
// }
|
||||
@@ -1 +0,0 @@
|
||||
export class CreateUserDto {}
|
||||
@@ -1,4 +0,0 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateUserDto } from './create-user.dto';
|
||||
|
||||
export class UpdateUserDto extends PartialType(CreateUserDto) {}
|
||||
@@ -1 +0,0 @@
|
||||
export class Register {}
|
||||
@@ -1,46 +0,0 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
Headers,
|
||||
} from '@nestjs/common';
|
||||
import { RegisterService } from './register.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
|
||||
@Controller('register')
|
||||
export class RegisterController {
|
||||
constructor(private readonly registerService: RegisterService) {}
|
||||
|
||||
@Post()
|
||||
create(
|
||||
@Headers('Authorization') accessToken: string,
|
||||
@Body() createUserDto: CreateUserDto,
|
||||
) {
|
||||
return this.registerService.create(accessToken, createUserDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Headers('Authorization') accessToken: string) {
|
||||
return this.registerService.findAll(accessToken);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.registerService.findOne(+id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
|
||||
return this.registerService.update(+id, updateUserDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.registerService.remove(+id);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { RegisterService } from './register.service';
|
||||
import { RegisterController } from './register.controller';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [RegisterController],
|
||||
providers: [RegisterService]
|
||||
})
|
||||
export class RegisterModule {}
|
||||
@@ -1,100 +0,0 @@
|
||||
import { Body, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
Configuration,
|
||||
CredentialRepresentation,
|
||||
UserRepresentation,
|
||||
UsersApi,
|
||||
} from '../../../keycloak/kc-client';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
|
||||
@Injectable()
|
||||
export class RegisterService {
|
||||
async create(accessToken: string, userRepresentation: UserRepresentation) {
|
||||
accessToken = accessToken.replace('Bearer ', '');
|
||||
const basePath = `${process.env.KC_BASE_URL}/auth/admin/realms`;
|
||||
const usersApi = new UsersApi({ basePath, accessToken });
|
||||
try {
|
||||
const registereredUser = await usersApi.realmUsersPost(
|
||||
process.env.KC_REALM,
|
||||
userRepresentation,
|
||||
);
|
||||
// console.log(registereredUser);
|
||||
|
||||
try {
|
||||
const newUserRegistered = await usersApi.realmUsersGet(
|
||||
process.env.KC_REALM,
|
||||
true,
|
||||
userRepresentation.email,
|
||||
);
|
||||
if (newUserRegistered.data.length > 0) {
|
||||
const userFound: UserRepresentation = newUserRegistered.data[0];
|
||||
console.log(userFound.id);
|
||||
|
||||
try {
|
||||
const credentialRepresentation: CredentialRepresentation = {temporary: false, value: "1234"}
|
||||
const setUserPassword = await usersApi.realmUsersIdResetPasswordPut(process.env.KC_REALM, userFound.id, credentialRepresentation)
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
// Ska nevoje te nsim email.
|
||||
const sendMailToCreatedUser =
|
||||
await usersApi.realmUsersIdSendVerifyEmailPut(
|
||||
process.env.KC_REALM,
|
||||
userFound.id,
|
||||
process.env.KC_CLIENT_ID,
|
||||
'http://localhost:3000',
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
console.log(sendMailToCreatedUser);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// console.log(error);
|
||||
return error.data;
|
||||
}
|
||||
|
||||
return registereredUser.data;
|
||||
// try {
|
||||
// const sendMail = await usersApi.realmUsersIdSendVerifyEmailPut(process.env.KC_REALM, registereredUser.data.id)
|
||||
// } catch (error) {
|
||||
|
||||
// }
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return error.response.data;
|
||||
}
|
||||
}
|
||||
|
||||
async findAll(accessToken: string) {
|
||||
accessToken = accessToken.replace('Bearer ', '');
|
||||
|
||||
const basePath = `${process.env.KC_BASE_URL}/auth/admin/realms`;
|
||||
const usersApi = new UsersApi({ basePath, accessToken });
|
||||
try {
|
||||
const users = await usersApi.realmUsersGet(process.env.KC_REALM);
|
||||
return users.data;
|
||||
} catch (error) {
|
||||
return error.data;
|
||||
}
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} register`;
|
||||
}
|
||||
|
||||
update(id: number, updateRegisterDto: UpdateUserDto) {
|
||||
return `This action updates a #${id} register`;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} register`;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,6 @@ import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(process.env.APP_PORT || '3000');
|
||||
await app.listen(3000);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
Reference in New Issue
Block a user