Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Commit

Permalink
#146 - fixed bug with authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsidlyarevich committed Mar 19, 2018
1 parent ad1b5ba commit f8aecb4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export class LoginFormComponent implements OnInit {
.subscribe(result => {
if (this.authenticationService.isLoggedIn()) {
this.loaderService.hide();
this.router.navigate([this.redirectUrl]);
if (result) {
this.router.navigate([this.redirectUrl]);
}
} else {
this.loaderService.hide();
this.notificationService.error('Can\'t log in');
Expand Down
41 changes: 24 additions & 17 deletions client/src/app/services/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { User } from '../models/user.model';
import { UserSocial } from '../models/user-social.model';
import { config } from '../config/config';
import { TokenService } from "./token.service";
import 'rxjs/add/operator/catch';

@Injectable()
export class AuthenticationService {
Expand All @@ -33,34 +34,34 @@ export class AuthenticationService {
.post<JwtResponse>(config.authApi, body, { headers: headers, observe: 'response' })
.map((data) => {
if (data && data.body.token) {
try {
this.tokenService.storeAuthenticationToken(JSON.stringify(data.body.token), rememberMe);
this.storeCurrentUser();
this.storeCurrentUserSocial();
return data;
} catch (error) {
console.log(error);
}
this.tokenService.storeAuthenticationToken(JSON.stringify(data.body.token), rememberMe);
return data.body.token;
}
}).mergeMap((token) => {
return this.storeCurrentUser();
}).mergeMap((user: User) => {
return this.storeCurrentUserSocial(user);
});
}

socialLoginWithToken(jwt, provider, rememberMe): Promise<any> {
if (jwt) {
this.tokenService.storeAuthenticationToken(JSON.stringify(jwt), rememberMe);
this.storeCurrentUser();
this.storeCurrentUserSocial();
this.storeCurrentUser()
.subscribe((response: User) => {
this.storeCurrentUserSocial(response);
});
this.$localStorage.store('provider', provider);
return Promise.resolve(jwt);
} else {
return Promise.reject('Login error');
}
}

private storeCurrentUser() {
this.http
private storeCurrentUser(): Observable<User> {
return this.http
.get<User>(config.userApi + '/me')
.subscribe((response) => {
.map((response) => {
this.$localStorage.store('user', response);
return response;
},
Expand All @@ -69,13 +70,19 @@ export class AuthenticationService {
});
}

private storeCurrentUserSocial() {
const url = config.userSocialApi.replace('${userId}', this.$localStorage.retrieve('user').id);
private storeCurrentUserSocial(user: User): Observable<boolean | {}> {
const url = config.userSocialApi.replace('${userId}', user.id);

this.http
return this.http
.get<UserSocial>(url)
.subscribe((response) => {
.map((response) => {
this.$localStorage.store('userSocial', response);
return true;
}).catch((error: HttpErrorResponse) => {
return new Observable((observer) => {
observer.next(true);
observer.complete();
});
});
}

Expand Down

0 comments on commit f8aecb4

Please sign in to comment.