Skip to content
Snippets Groups Projects
Commit 274de94f authored by Jonny Ngo Luong's avatar Jonny Ngo Luong
Browse files

refactor: lagt til kommentarer der det er nødvendig i frontend

parent 3dc3322a
No related branches found
No related tags found
1 merge request!16Refactor: lagt til kommentarer der det er nødvendig i frontend
......@@ -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,11 +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> {
......@@ -88,7 +102,6 @@ export class AuthService {
}
);
}
private register_user(user: User) {
return this.http.post(this.registrationUrl, user.serialize());
}
......
......@@ -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;
......
......@@ -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;
......
......@@ -22,7 +22,7 @@ export class UserRegistrationFormComponent implements OnInit {
}
/**
* Validates form.
* Validates the form
*/
checkForm(): boolean {
if (this.username == "") {
......@@ -43,7 +43,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()) {
......@@ -53,7 +53,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("/");
......@@ -64,7 +64,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;
......
......@@ -76,55 +76,5 @@ describe('UserService', () => {
});
});
describe('addUser', () => {
it('should add an user', () => {
const user = new User({
userId: 1,
username: "zorg",
email: "blob@planet.us",
password: "Hyttepine",
create_time: 1613552549000,
});
// Adds user
service.addUser(user)
.then(post => {})
.catch(error => {
fail();
});
// Mocks and checks HTTP request
const req = httpMock.expectOne("api/user/");
expect(req.request.method).toBe("POST");
expect(req.request.body).toEqual(user.serialize());
req.flush({
data: [{
status: "success"
}]
});
});
it('should reject on http error', () => {
const user = new User({
userId: 1,
username: "zorg",
email: "blob@planet.us",
password: "Hyttepine",
create_time: 1613552549000,
});
// Adds user, gets HTTP error, should catch
service.addUser(user).then(user => {
fail();
}).catch(error => {});
// Mocks and checks HTTP request
const req = httpMock.expectOne("api/user/");
expect(req.request.method).toBe("POST");
expect(req.request.body).toEqual(user.serialize());
req.error(new ErrorEvent("400"));
});
});
});
......@@ -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);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment