diff --git a/client/src/app/authentication/auth.service.ts b/client/src/app/authentication/auth.service.ts index 838b8f647ecc82a43bbf4108cb1e01b0c13b0434..cab69f3b5dea1554e09e0501f3922d7a09356a23 100644 --- a/client/src/app/authentication/auth.service.ts +++ b/client/src/app/authentication/auth.service.ts @@ -18,6 +18,9 @@ export class AuthService { constructor(private http: HttpClient, private router: Router) { } + /** + * Logins an user, if given correct combination of username and password. + */ login(body: IUserLogin): Promise<string> { return new Promise<string>( (resolve, reject) => { @@ -36,14 +39,20 @@ export class AuthService { ); } private login_user(body: IUserLogin) { + // Pipes output to setSession function if a valid user is returned return this.http.post(this.loginUrl, body).pipe( tap(res =>this.setSession(res)), - shareReplay());; + shareReplay()); } + // Set authentication token on localStorage if a valid user is received private setSession(authResult) { console.log(authResult); localStorage.setItem('token', authResult.token); } + + /** + * Checks validity of token, redirects to homepage and removes it if it is expired + */ checkTokenExpiration() { const token = localStorage.getItem("token"); if (token) { @@ -64,12 +73,16 @@ export class AuthService { this.router.navigateByUrl("/") return false } + + /** + * Logout an user and redirects to the homepage + */ logout() { localStorage.removeItem("token"); this.router.navigateByUrl("/") } - /** + /** * Register an user, if not duplicate, add to database. */ registerUser(user: User): Promise<string> { @@ -89,7 +102,6 @@ export class AuthService { } ); } - private register_user(user: User) { return this.http.post(this.registrationUrl, user.serialize()); } diff --git a/client/src/app/users/user-login-form/user-login-form.component.ts b/client/src/app/users/user-login-form/user-login-form.component.ts index 328d380f561a08a0c64bf9cbc458acba399aef2a..3f4cefddfdbaf340b8a983de899ed4a6b96b50cf 100644 --- a/client/src/app/users/user-login-form/user-login-form.component.ts +++ b/client/src/app/users/user-login-form/user-login-form.component.ts @@ -21,7 +21,7 @@ export class UserLoginFormComponent implements OnInit { } /** - * Validates form. + * Validates the form */ checkForm(): boolean { if (this.username == "") { @@ -38,7 +38,7 @@ export class UserLoginFormComponent implements OnInit { } /** - * Login the user if it is valid. + * Login the user if it is valid */ loginUser() { if (this.checkForm()) { @@ -47,26 +47,18 @@ export class UserLoginFormComponent implements OnInit { password: this.password, }; - // Login the user + // Logins the user this.authService.login(request).then(status => { console.log("User login1: " + JSON.stringify(status)); this.router.navigateByUrl("/"); }).catch(error => { console.log("Error user login: " + error); }); - /* Old - this.userService.login(request).then(status => { - console.log("User login2: " + JSON.stringify(status)); - this.router.navigateByUrl("/"); - }).catch(error => { - console.log("Error adding user: " + error); - }); - */ } } /** - * Sets a status message. + * Sets the status message */ setStatusMessage(message: string) { this.statusMessage = message; diff --git a/client/src/app/users/user-profile/user-profile.component.ts b/client/src/app/users/user-profile/user-profile.component.ts index 53be4cceb4ffa0990d2be78603cd6f42361c75a3..ce10985b22048f6d0771c07ff0d54e02b6cde92d 100644 --- a/client/src/app/users/user-profile/user-profile.component.ts +++ b/client/src/app/users/user-profile/user-profile.component.ts @@ -18,9 +18,10 @@ export class UserProfileComponent implements OnInit { ngOnInit(): void { // Check for token expiration if (this.authService.checkTokenExpiration()) { // redirects to "/" if token is expired + // Get user data from JWT token const token = localStorage.getItem('token'); - // Get user data from JWT token const user_data = JSON.parse(atob(token.split(".")[1])).data[0]; + // Gets all user information and displays them in the component this.userService.getUser(user_data.userId).then(user => { this.user = user; diff --git a/client/src/app/users/user-registration-form/user-registration-form.component.ts b/client/src/app/users/user-registration-form/user-registration-form.component.ts index 9bccc2204127c5793421142e8d11a91dda2a4a4e..e1f7cf17bc0ca59392cb383742a5820325dc2113 100644 --- a/client/src/app/users/user-registration-form/user-registration-form.component.ts +++ b/client/src/app/users/user-registration-form/user-registration-form.component.ts @@ -23,7 +23,7 @@ export class UserRegistrationFormComponent implements OnInit { } /** - * Validates form. + * Validates the form */ checkForm(): boolean { if (this.username == "") { @@ -52,7 +52,7 @@ export class UserRegistrationFormComponent implements OnInit { } /** - * Publishes user if it is valid. + * Publishes and registers the user if given arguments are valid */ registerUser() { if (this.checkForm()) { @@ -62,7 +62,7 @@ export class UserRegistrationFormComponent implements OnInit { password: this.password, }); - // Adds user to database and changes page afterwards + // Adds user to database and redirects to the homepage afterwards this.authService.registerUser(newUser).then(status => { console.log("User was added: " + JSON.stringify(status)); this.router.navigateByUrl("/login"); @@ -73,7 +73,7 @@ export class UserRegistrationFormComponent implements OnInit { } /** - * Sets a status message. + * Sets the status message for user feedback on form submit */ setStatusMessage(message: string) { this.statusMessage = message; diff --git a/client/src/app/users/user.service.ts b/client/src/app/users/user.service.ts index b56d03849b5795ecce38a162d4a0836678a28288..f4def23c7d6289232ae86ea9d6f0c0dee50b3a35 100644 --- a/client/src/app/users/user.service.ts +++ b/client/src/app/users/user.service.ts @@ -16,7 +16,7 @@ export class UserService { constructor(private http: HttpClient) { } /** - * Get user from database by id. + * Get an user from the database by id. */ getUser(id: number): Promise<User> { return new Promise<User>( @@ -40,7 +40,6 @@ export class UserService { } ); } - private get_user(id: number) { return this.http.get(this.userUrl + id); } @@ -74,7 +73,6 @@ export class UserService { } ); } - private get_all_users() { return this.http.get(this.userUrl); }