Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Make a call to our backend with user credentials
- * Verify presence of JWT in the response's header
- * Store it in local storage and return true for a successful login or false to a failed one
- * @param {User} user
- * @returns {Observable<boolean>}
- */
- login(user: User): Observable<boolean> {
- return this.http.post('/api/login', JSON.stringify({ mail: user.mail, password: user.password }))
- .map(response => {
- // login successful if there's a jwt token in the response
- let token = response.headers.get('Authorization');
- if (token) {
- // set token property
- this.token = token;
- // store username and jwt token in local storage to keep user logged in between page refreshes
- localStorage.setItem('token', token);
- // return true to indicate successful login
- return true;
- } else {
- // return false to indicate failed login
- return false;
- }
- })
- .catch(AuthenticationService.handleError);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement