Skip to content

Commit

Permalink
Merge pull request #76 from pkorchak/add-registration
Browse files Browse the repository at this point in the history
feat: Call /users/register endpoint after SocialAuthService login
  • Loading branch information
pkorchak committed Sep 3, 2023
2 parents 936a53b + f1decab commit 36c8ace
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 119 deletions.
261 changes: 147 additions & 114 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"scripts": {
"ng": "ng",
"start": "set NODE_OPTIONS=--openssl-legacy-provider && ng serve",
"start": "set NODE_OPTIONS=--openssl-legacy-provider && ng serve --proxy-config=proxy.config.json",
"build": "ng build --configuration production",
"lint": "ng lint",
"test": "ng test",
Expand All @@ -29,7 +29,7 @@
"ng-mocks": "^14.10.0",
"ng-zorro-antd": "^16.0.0",
"ngx-scrollbar": "12.0.0",
"rxjs": "^6.5.5",
"rxjs": "^7.8.1",
"tslib": "^2.0.0",
"zone.js": "~0.13.0"
},
Expand Down
10 changes: 10 additions & 0 deletions proxy.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"/api/*": {
"target": " https://03z6w06wwd.execute-api.us-east-1.amazonaws.com/Stage",
"secure": false,
"changeOrigin": true,
"pathRewrite": {
"^/api": ""
}
}
}
13 changes: 13 additions & 0 deletions src/app/model/dto/rq/register-user-rq-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface RegisterUserRqDto {
firstName: string;
lastName: string;
email: string;
emailVerified?: boolean;
photoUrl?: string;
provider: IdentityProvider;
}

export enum IdentityProvider {
INTERNAL = 'INTERNAL', // TODO Add registration by email/password inside of the app
GOOGLE = 'GOOGLE'
}
4 changes: 4 additions & 0 deletions src/app/model/dto/rs/register-user-rs-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface RegisterUserRsDto {
id?: string;
message: string;
}
39 changes: 39 additions & 0 deletions src/app/services/api/http.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import { catchError } from 'rxjs/operators';

@Injectable({providedIn: 'root'})
export class HttpService {

private headers: HttpHeaders = new HttpHeaders({'Content-Type': 'application/json'});

constructor(private http: HttpClient,
private router: Router) {
}

public post(url: string, body: unknown, params?: HttpParams): Observable<unknown> {
return this.handleResponse(this.http.post(url, body, {headers: this.headers, ...params}));
}

public get(url: string, params?: HttpParams): Observable<unknown> {
return this.handleResponse(this.http.get(url, {headers: this.headers, ...params}));
}

public patch(url: string, body: unknown, params?: HttpParams): Observable<unknown> {
return this.handleResponse(this.http.patch(url, body, {headers: this.headers, ...params}));
}

private handleResponse(response: Observable<unknown>): Observable<unknown> {
return response.pipe(catchError(err => this.catchError(err)));
}

private catchError(error: HttpErrorResponse): Observable<void> {
if (error.status === 401) {
this.router.navigateByUrl('/login');
}
// TODO Show notification with error text for the user
throw new Error(`${error.statusText}\n${error.message}`);
}
}
13 changes: 13 additions & 0 deletions src/app/services/api/users-http.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable } from '@angular/core';
import { RegisterUserRqDto } from '@model/dto/rq/register-user-rq-dto';
import { RegisterUserRsDto } from '@model/dto/rs/register-user-rs-dto';
import { Observable } from 'rxjs';
import { HttpService } from './http.service';

@Injectable({ providedIn: 'root' })
export class UsersHttpService extends HttpService {

public register(user: RegisterUserRqDto): Observable<RegisterUserRsDto> {
return this.post('/api/users/register', user) as Observable<RegisterUserRsDto>;
}
}
23 changes: 20 additions & 3 deletions src/app/widgets/auth/auth.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { SocialAuthService, SocialUser } from '@abacritt/angularx-social-login';
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { IdentityProvider, RegisterUserRqDto } from '@model/dto/rq/register-user-rq-dto';
import { RegisterUserRsDto } from '@model/dto/rs/register-user-rs-dto';
import { UsersHttpService } from '@services/api/users-http.service';

@Component({
selector: 'app-auth',
Expand All @@ -10,7 +13,10 @@ export class AuthComponent implements OnInit {

@Output() signedIn = new EventEmitter();

constructor(private socialAuthService: SocialAuthService) { }
constructor(
private socialAuthService: SocialAuthService,
private usersHttpService: UsersHttpService) {
}

ngOnInit() {
// TODO Add login by email
Expand All @@ -21,7 +27,18 @@ export class AuthComponent implements OnInit {
this.socialAuthService.authState.subscribe((user: SocialUser) => {
if (user && user.idToken != localStorage.getItem('token')) {
localStorage.setItem('token', user.idToken);
this.signedIn.emit();

this.usersHttpService.register({
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
photoUrl: user.photoUrl,
provider: user.provider as IdentityProvider
} as RegisterUserRqDto)
.subscribe((rs: RegisterUserRsDto) => {
localStorage.setItem('userId', rs.id || '');
this.signedIn.emit();
});
}
});
}
Expand Down

0 comments on commit 36c8ace

Please sign in to comment.