Skip to content

Instantly share code, notes, and snippets.

import { AuthConfig } from './auth.config';
import { Inject, Injectable } from '@nestjs/common';
import {
AuthenticationDetails,
CognitoUser,
CognitoUserPool,
CognitoUserAttribute,
} from 'amazon-cognito-identity-js';
@Injectable()
import { BadRequestException, Body, Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('register')
async register(
@Body() registerRequest: { name: string; password: string; email: string },
@jacobdo2
jacobdo2 / main.ts
Created June 8, 2020 09:24
Adding node-fetch to main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
global['fetch'] = require('node-fetch');
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
@jacobdo2
jacobdo2 / auth.controller.ts
Created June 8, 2020 09:13
AuthController
import { BadRequestException, Body, Controller, Post } from '@nestjs/common';
import { AuthService } from './auth.service';
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Post('login')
async login(@Body() authenticateRequest: { name: string, password: string }) {
@jacobdo2
jacobdo2 / auth.service.ts
Last active June 8, 2020 09:08
AuthService
import { AuthConfig } from './auth.config';
import { Inject, Injectable } from '@nestjs/common';
import {
AuthenticationDetails,
CognitoUser,
CognitoUserPool,
} from 'amazon-cognito-identity-js';
@Injectable()
export class AuthService {
@jacobdo2
jacobdo2 / auth.module.ts
Created June 8, 2020 09:02
AuthConfig provider added
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthConfig } from './auth.config';
@Module({
providers: [AuthService, AuthConfig]
})
export class AuthModule {}
@jacobdo2
jacobdo2 / .env
Last active June 8, 2020 08:34
Cognito credentials in .env
COGNITO_USER_POOL_ID=eu-central-1_AadYWQBJE
COGNITO_CLIENT_ID=1bk2sqm1453tu5g7ldkg5vk23u
COGNITO_REGION=eu-central-1
@jacobdo2
jacobdo2 / app.module.ts
Last active June 8, 2020 08:41
Adding ConfigModule ot app.module.ts
import { Module } from '@nestjs/common';
import { AuthModule } from './auth/auth.module';
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [
AuthModule,
ConfigModule.forRoot({
isGlobal: true,
}),
@jacobdo2
jacobdo2 / auth.config.ts
Created June 8, 2020 08:07
auth.config.ts content
import { Injectable } from '@nestjs/common';
@Injectable()
export class AuthConfig {
public userPoolId: string = process.env.COGNITO_USER_POOL_ID;
public clientId: string = process.env.COGNITO_CLIENT_ID;
public region: string = process.env.COGNITO_REGION;
public authority = `https://cognito-idp.${process.env.COGNITO_REGION}.amazonaws.com/${process.env.COGNITO_USER_POOL_ID}`;
}