Feature: Added Login / Register Feature #3
|
@ -7,7 +7,7 @@ export class CorsMiddleware implements NestMiddleware {
|
|||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
public use(req: Request, res: Response, next: NextFunction): void {
|
||||
if (this.configService.get<string>('NODE_ENV') === 'production') {
|
||||
if (this.configService.get<string>('NODE_ENV') === 'development') {
|
||||
const allowedOrigin = this.configService.get<string>('CORS_ALLOW_ORIGIN');
|
||||
|
||||
if (req.headers.origin === allowedOrigin) {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { CommonModule } from '@angular/common';
|
||||
import { HttpClientModule } from '@angular/common/http';
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
|
@ -21,6 +22,8 @@ import { CheckboxModule } from 'primeng/checkbox';
|
|||
import { InputTextModule } from 'primeng/inputtext';
|
||||
import { PasswordModule } from 'primeng/password';
|
||||
|
||||
import { AuthService } from '../../shared/service';
|
||||
import { LoginCredentials } from '../../shared/types';
|
||||
import {
|
||||
customEmailValidator,
|
||||
customPasswordValidator,
|
||||
|
@ -39,7 +42,9 @@ type AuthAction = 'register' | 'signup';
|
|||
ButtonModule,
|
||||
CheckboxModule,
|
||||
PasswordModule,
|
||||
HttpClientModule,
|
||||
],
|
||||
providers: [AuthService],
|
||||
templateUrl: './register-root.component.html',
|
||||
styleUrl: './register-root.component.scss',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
|
@ -53,7 +58,10 @@ export class RegisterRootComponent implements OnInit {
|
|||
public passwordInvalid: WritableSignal<string | null> = signal(null);
|
||||
public termsInvalid: WritableSignal<string | null> = signal(null);
|
||||
|
||||
public constructor(private readonly formBuilder: FormBuilder) {
|
||||
public constructor(
|
||||
private readonly formBuilder: FormBuilder,
|
||||
private readonly authService: AuthService
|
||||
) {
|
||||
effect(() => {
|
||||
if (this.form) {
|
||||
if (this.isRegisterSignal()) {
|
||||
|
@ -197,11 +205,11 @@ export class RegisterRootComponent implements OnInit {
|
|||
}
|
||||
}
|
||||
|
||||
private signin(data: unknown): void {
|
||||
console.log(data);
|
||||
private signin(logiCredentials: LoginCredentials): void {
|
||||
this.authService.signin(logiCredentials);
|
||||
}
|
||||
|
||||
private register(data: unknown): void {
|
||||
console.log(data);
|
||||
private register(logiCredentials: LoginCredentials): void {
|
||||
this.authService.signup(logiCredentials);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { LoginCredentials, Tokens } from '../types';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthService {
|
||||
private isAuthenticated: boolean = false;
|
||||
private access_token: string | null = null;
|
||||
private refresh_token: string | null = null;
|
||||
|
||||
public constructor(
|
||||
private readonly httpClient: HttpClient,
|
||||
private readonly router: Router
|
||||
) {}
|
||||
|
||||
public signin(credentials: LoginCredentials): void {
|
||||
this.httpClient
|
||||
.post<Tokens>(environment.api.base + '/api/auth/signin', credentials)
|
||||
.subscribe((response: Tokens) => {
|
||||
this.access_token = response.access_token;
|
||||
this.refresh_token = response.refresh_token;
|
||||
});
|
||||
}
|
||||
|
||||
public signup(credentials: LoginCredentials): void {
|
||||
this.httpClient
|
||||
.post<Tokens>(environment.api.base + '/api/auth/signup', credentials)
|
||||
.subscribe((response: Tokens) => {
|
||||
this.access_token = response.access_token;
|
||||
this.refresh_token = response.refresh_token;
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
export * from './auth.service';
|
|
@ -0,0 +1,2 @@
|
|||
export * from './login-credentials';
|
||||
export * from './tokens';
|
|
@ -0,0 +1,4 @@
|
|||
export type LoginCredentials = {
|
||||
email: string;
|
||||
password: string;
|
||||
};
|
|
@ -0,0 +1,4 @@
|
|||
export type Tokens = {
|
||||
access_token: string;
|
||||
refresh_token: string;
|
||||
};
|
|
@ -1,5 +1,8 @@
|
|||
export const environment = {
|
||||
production: false,
|
||||
api: {
|
||||
base: 'http://localhost:3000',
|
||||
},
|
||||
oauth: {
|
||||
clinetId: 'app_FLXnxSBnnaKkXoYCgk3J62iA',
|
||||
redirectUri: 'https://commonly-hot-airedale.ngrok-free.app/oauth',
|
||||
|
|
Loading…
Reference in New Issue