Skip to content

Commit

Permalink
Don't automatically create user on oauth signup
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Mar 26, 2024
1 parent 23cc919 commit 91a821c
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 89 deletions.
7 changes: 4 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { OldLoginComponent } from './login/old-login.component';
import { HttpClientModule } from '@angular/common/http';
import { httpInterceptorProviders } from '../interceptors';
import { FormsModule } from '@angular/forms';
Expand Down Expand Up @@ -58,6 +57,7 @@ import { LoginComponent } from './auth/login/login.component';
import { HeaderBarVerifyEmailComponent } from './_layout/header-bar/header-bar-verify-email/header-bar-verify-email.component';
import { PasswordResetComponent } from './auth/password-reset/password-reset.component';
import { ForgotPasswordComponent } from './auth/forgot-password/forgot-password.component';
import { LoginOauthComponent } from './auth/login-oauth/login-oauth.component';

const appRoutes: Routes = [
{
Expand All @@ -78,7 +78,7 @@ const appRoutes: Routes = [
},
{
path: 'login/:service',
component: OldLoginComponent,
component: LoginOauthComponent,
data: { skipRouteLocalization: true },
},
{
Expand Down Expand Up @@ -125,7 +125,7 @@ const appRoutes: Routes = [
@NgModule({
declarations: [
AppComponent,
OldLoginComponent,
LoginOauthComponent,
HomepageComponent,
AboutComponent,
PatronsComponent,
Expand Down Expand Up @@ -157,6 +157,7 @@ const appRoutes: Routes = [
HeaderBarVerifyEmailComponent,
PasswordResetComponent,
ForgotPasswordComponent,
LoginOauthComponent,
],
imports: [
BrowserModule,
Expand Down
File renamed without changes.
File renamed without changes.
110 changes: 110 additions & 0 deletions src/app/auth/login-oauth/login-oauth.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Component, OnInit } from '@angular/core';
import { UserService } from '../../../services/user.service';
import { ActivatedRoute, Router } from '@angular/router';
import { NwbAlertConfig, NwbAlertService } from '@wizishop/ng-wizi-bulma';
import { TranslateService } from '@ngx-translate/core';
import { LoadingBarService } from '../../../services/loading-bar.service';
import { AuthService } from '../../../services/auth.service';
import { firstValueFrom } from 'rxjs';
import { LoginResponse, LoginResponseStatus } from '../../../model/auth';

@Component({
selector: 'app-login-oauth',
templateUrl: './login-oauth.component.html',
styleUrls: ['./login-oauth.component.scss']
})
export class LoginOauthComponent implements OnInit {

constructor(
private authService: AuthService,
private userService: UserService,
private route: ActivatedRoute,
private router: Router,
private toastr: NwbAlertService,
private translateService: TranslateService,
private loader: LoadingBarService
) { }

ngOnInit(): void {
setTimeout(() => {
this.loader.setLoading(true);
}, 0);

this.route.params.subscribe(params => {
this.route.queryParams.subscribe(queryParams => {
const service = params['service'];
const code = queryParams['code'];

this.loginTo(service, code);
});
});
}

async loginTo(service: string, code: string): Promise<void> {
try {
const response = await firstValueFrom(this.authService.performOauthLogin({ service, code }));

switch (response.status) {
case LoginResponseStatus.LOGIN_SUCCESS:
this.userService.token = response.token;
this.userService.me().add(() => {
if (this.userService.user.mail) {
const item = localStorage.getItem('prev_loc') || '/';
localStorage.removeItem('prev_loc');

this.router.navigate([item]);
}
});
return;
}
} catch (e: any) {
const { error }: { error: LoginResponse } = e;

switch (error.status) {
case LoginResponseStatus.ACCOUNT_DISABLED:
this.disabledAccountToast();
return;
case LoginResponseStatus.OAUTH_ACCOUNT_NOT_FOUND:
this.unknownAccountToast();
return;
default:
this.router.navigate(['/']);
this.translateService.get('alert.user.login.error').subscribe((res: string) => {
const alertConfig: NwbAlertConfig = {
message: res + '\n\nCode: ' + error.status,
duration: 5000,
position: 'is-right',
color: 'is-warning'
};
this.toastr.open(alertConfig);
});
}
}
}

unknownAccountToast() {
this.router.navigate(['/register']);

// TODO: translation
const alertConfig: NwbAlertConfig = {
message: 'Account not found, please register for a new account or sync with an existing account.',
duration: 8000,
position: 'is-right',
color: 'is-warning'
};
this.toastr.open(alertConfig);
}

disabledAccountToast() {
this.router.navigate(['/']);
this.translateService.get('alert.user.login.disabledAccount').subscribe((res: string) => {
const alertConfig: NwbAlertConfig = {
message: res,
duration: 5000,
position: 'is-right',
color: 'is-warning'
};
this.toastr.open(alertConfig);
});
}
}
83 changes: 0 additions & 83 deletions src/app/login/old-login.component.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/assets/i18n
Submodule i18n updated from f4e978 to a8696c
7 changes: 7 additions & 0 deletions src/model/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export interface LoginDetails {
twoFactorCode: string | null;
}

export interface OauthLoginDetails {
service: string;
code: string;
}

export interface LoginResponse {
token: string | null;
status: LoginResponseStatus;
Expand All @@ -13,6 +18,8 @@ export enum LoginResponseStatus {
MFA_REQUIRED = 'MFA_REQUIRED',
MFA_INVALID = 'MFA_INVALID',
LOGIN_SUCCESS = 'LOGIN_SUCCESS',
ACCOUNT_DISABLED = 'ACCOUNT_DISABLED',
OAUTH_ACCOUNT_NOT_FOUND = 'OAUTH_ACCOUNT_NOT_FOUND',
USERNAME_PASSWORD_INCORRECT = 'USERNAME_PASSWORD_INCORRECT',
}

Expand Down
8 changes: 6 additions & 2 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { NwbAlertService } from '@wizishop/ng-wizi-bulma';
import { Router } from '@angular/router';
import { firstValueFrom, Observable } from 'rxjs';
import { BaseService } from './BaseService';
import { InitMFADto, LoginDetails, LoginResponse } from '../model/auth';
import { InitMFADto, LoginDetails, LoginResponse, OauthLoginDetails } from '../model/auth';
import { environment } from '../environments/environment';
import { SignupDto, SignupResponseDto } from '../model/dto/signup-dto';

Expand Down Expand Up @@ -71,6 +71,10 @@ export class AuthService extends BaseService {
}&scope=identity&redirect_uri=${this.getSyncRedirectUri()}patreon`;
}

performOauthLogin(details: OauthLoginDetails): Observable<LoginResponse> {
return this.http.post<LoginResponse>(this.v2Url('login/service'), details);
}

performLogin(details: LoginDetails): Observable<LoginResponse> {
details.twoFactorCode = details.twoFactorCode || null;

Expand Down

0 comments on commit 91a821c

Please sign in to comment.