feat: add user creation on login (wip)
This commit is contained in:
parent
44c7d8be57
commit
793f3f6834
13 changed files with 196 additions and 9 deletions
|
@ -27,7 +27,7 @@ export const initializeKeycloak = (keycloak: KeycloakService) => async () =>
|
|||
onLoad: 'check-sso',
|
||||
silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html',
|
||||
checkLoginIframe: false,
|
||||
redirectUri: 'http://localhost:4200',
|
||||
redirectUri: window.location.origin + '/login/success',
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -2,12 +2,17 @@ import { Routes } from '@angular/router';
|
|||
import { LandingComponent } from './feature/landing/landing.component';
|
||||
import { HomeComponent } from './feature/home/home.component';
|
||||
import { authGuard } from './auth.guard';
|
||||
import { LoginSuccessComponent } from './login-success/login-success.component';
|
||||
|
||||
export const routes: Routes = [
|
||||
{
|
||||
path: '',
|
||||
component: LandingComponent,
|
||||
},
|
||||
{
|
||||
path: 'login/success',
|
||||
component: LoginSuccessComponent,
|
||||
},
|
||||
{
|
||||
path: 'home',
|
||||
component: HomeComponent,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from '@angular/router';
|
||||
import { ActivatedRouteSnapshot, CanActivateFn, Router, RouterStateSnapshot } from '@angular/router';
|
||||
import { inject } from '@angular/core';
|
||||
import { KeycloakService } from 'keycloak-angular';
|
||||
|
||||
|
@ -7,17 +7,13 @@ export const authGuard: CanActivateFn = async (
|
|||
state: RouterStateSnapshot
|
||||
) => {
|
||||
const keycloakService = inject(KeycloakService);
|
||||
const isLoggedIn = keycloakService.isLoggedIn();
|
||||
const router = inject(Router);
|
||||
|
||||
if (isLoggedIn) {
|
||||
if (keycloakService.isLoggedIn()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const baseurl = window.location.origin;
|
||||
|
||||
keycloakService.login({
|
||||
redirectUri: `${baseurl}${state.url}`,
|
||||
});
|
||||
router.navigate(['']);
|
||||
|
||||
return false;
|
||||
};
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
<p>login-success works!</p>
|
25
frontend/src/app/login-success/login-success.component.ts
Normal file
25
frontend/src/app/login-success/login-success.component.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { Component, inject, OnInit } from '@angular/core';
|
||||
import { UserService } from '../service/user.service';
|
||||
import { KeycloakService } from 'keycloak-angular';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login-success',
|
||||
standalone: true,
|
||||
imports: [],
|
||||
templateUrl: './login-success.component.html',
|
||||
styleUrl: './login-success.component.css'
|
||||
})
|
||||
export class LoginSuccessComponent implements OnInit{
|
||||
private userService: UserService = inject(UserService);
|
||||
private keycloakService: KeycloakService = inject(KeycloakService);
|
||||
private router: Router = inject(Router);
|
||||
|
||||
async ngOnInit() {
|
||||
const userProfile = await this.keycloakService.loadUserProfile();
|
||||
const user = this.userService.getCurrentUser(userProfile);
|
||||
sessionStorage.setItem('user', JSON.stringify(user));
|
||||
|
||||
// this.router.navigate(['']);
|
||||
}
|
||||
}
|
34
frontend/src/app/service/user.service.ts
Normal file
34
frontend/src/app/service/user.service.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { KeycloakProfile } from 'keycloak-js';
|
||||
import { async } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class UserService {
|
||||
private http: HttpClient = inject(HttpClient);
|
||||
|
||||
public getUser(id: string) {
|
||||
return this.http.get<{ keycloakId: string, username: string } | null>(`/backend/user/${id}`);
|
||||
}
|
||||
|
||||
public createUser(id: string, username: string) {
|
||||
return this.http.post<{ keycloakId: string, username: string }>('/backend/user', {
|
||||
keycloakId: id,
|
||||
username: username,
|
||||
});
|
||||
}
|
||||
|
||||
public async getCurrentUser(userProfile: KeycloakProfile) {
|
||||
if (userProfile.id == null) {
|
||||
return;
|
||||
}
|
||||
return await this.getUser(userProfile.id).toPromise().then(async user => {
|
||||
if (user) {
|
||||
return user;
|
||||
}
|
||||
return await this.createUser(userProfile.id ?? '', userProfile.username ?? '').toPromise();
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue