Skip to content

Commit

Permalink
Repeated relogs work now
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanja-4732 committed Sep 3, 2019
1 parent aca7292 commit 5780f2b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
11 changes: 7 additions & 4 deletions backend/src/app/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,13 @@ export class Authentication {
/**
* The user from the projection
*/
const user = this.es.users.find((user: User) => {
// Find the user in the projection with a matching email
return user.uuid === verified.sub;
});
const user = Object.assign(
{},
this.es.users.find((user: User) => {
// Find the user in the projection with a matching email
return user.uuid === verified.sub;
}),
);

// Check for not logged in requests
if (user == undefined) {
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/app/components/account/account.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { HttpErrorResponse } from "@angular/common/http";
styleUrls: ["./account.component.scss"]
})
export class AccountComponent implements OnInit {
oof: boolean = false;
loading: boolean = true;
unauthorized: boolean = false;
oof = false;
loading = true;
unauthorized = false;

user: User;

form: FormGroup;
error: { status: string; user: User };
disable2FA: boolean = false;
disable2FA = false;

constructor(
private as: AuthService,
Expand All @@ -29,7 +29,7 @@ export class AccountComponent implements OnInit {
private fb: FormBuilder
) {
this.createForm();
this.form.patchValue({ email: this.route.snapshot.params["email"] });
this.form.patchValue({ email: this.route.snapshot.params.email });
}
ngOnInit(): void {
this.loadUser().then();
Expand All @@ -53,7 +53,7 @@ export class AccountComponent implements OnInit {

async loadUser(): Promise<void> {
try {
this.user = await this.as.getUser();
this.user = ((await this.as.getUser()).user as unknown) as User; // TODO FixMe
console.log(this.user);

this.form.patchValue(this.user);
Expand Down Expand Up @@ -84,7 +84,7 @@ export class AccountComponent implements OnInit {
async save(): Promise<void> {
try {
this.error = await this.as.saveUser({
id: this.user.id,
uuid: this.user.uuid,
name: this.form.value.name,
email: this.form.value.email,
pwd: this.form.value.passwords.password,
Expand Down
19 changes: 13 additions & 6 deletions frontend/src/app/components/welcome/welcome.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Component, OnInit } from "@angular/core";
import { AuthService } from "../../services/auth/auth.service";
import {
AuthService,
GetStatusResponse
} from "../../services/auth/auth.service";
import { InventoryService } from "../../services/inventory/inventory.service";
import { User } from "../../models/user/user";

Expand All @@ -12,9 +15,9 @@ export class WelcomeComponent implements OnInit {
/**
* If the user is logged in
*/
loggedIn: boolean = true;
loading: boolean = true;
user: User;
loggedIn = true;
loading = true;
user: { uuid: string; email: string; name?: string };

constructor(private as: AuthService) {}

Expand All @@ -25,8 +28,12 @@ export class WelcomeComponent implements OnInit {

async testLogin(): Promise<void> {
try {
this.user = await this.as.getUser();
this.loggedIn = true;
console.log("hello?");
const res = await this.as.getUser();
console.log(res);

this.loggedIn = res.authorized;
this.user = res.user;
} catch (error) {
this.loggedIn = false;
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/models/user/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class User {
id: number;
uuid: number;
name: string;
email: string;
tfaEnabled: boolean;
Expand Down
23 changes: 15 additions & 8 deletions frontend/src/app/services/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,13 @@ export class AuthService {
.toPromise();
}

async getUser(): Promise<User> {
const res: { status: string; user: User } = await this.http
.get<{ status: string; user: User }>(this.baseUrl + "/account")
/**
* Fetches info from the server about the current login status
*/
async getUser(): Promise<GetStatusResponse> {
return await this.http
.get<GetStatusResponse>(this.baseUrl + "/authentication/status")
.toPromise();

if (res.status !== "Authenticated") {
throw new Error("Not signed in");
}
return res.user;
}

async saveUser(user: User): Promise<{ status: string; user: User }> {
Expand Down Expand Up @@ -102,3 +100,12 @@ interface RegisterResponse {
message: string;
email: string;
}

export interface GetStatusResponse {
authorized: boolean;
user: {
uuid: string;
email: string;
name?: string;
};
}

0 comments on commit 5780f2b

Please sign in to comment.