Skip to content

[feat] saving logged user to localStorage #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Date: 12/18/15
* Time: 10:34 AM
*/
import {Injectable, EventEmitter} from "@angular/core";
import {WindowService} from "./window.service";
import {Http, Headers} from "@angular/http";
import { Injectable, EventEmitter } from "@angular/core";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that the formatting stay consistent with the rest of the code; no spaces inside the braces.

import { WindowService } from "./window.service";
import { Http, Headers } from "@angular/http";

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -39,6 +39,15 @@ export class AuthService {
.replace('__scopes__', config.scopes);
this.oAuthUserUrl = config.userInfoUrl;
this.oAuthUserNameField = config.userInfoNameField;

this.token = localStorage.getItem('access_token');
if (this.token) {
this.authenticated = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there is a problem here, though I like what you are doing in general. I don't think you can set this.authenticated to true unless a) there is an expires value in local storage and b) that value has not passed already. We can't set auth to true if there's no expires value, and we can't set it to true if that expires value has already expired. If you can fix that up, I'll accept the new merge request.

if (localStorage.getItem('expires')) {
this.expires = localStorage.getItem('expires');
this.startExpiresTimer(this.expires);
}
}
})
}

Expand Down Expand Up @@ -69,10 +78,12 @@ export class AuthService {

this.token = parsed.access_token;
if (this.token) {
localStorage.setItem('access_token', this.token);
this.authenticated = true;
this.startExpiresTimer(expiresSeconds);
this.expires = new Date();
this.expires = this.expires.setSeconds(this.expires.getSeconds() + expiresSeconds);
this.startExpiresTimer(this.expires);
localStorage.setItem('expires', this.expires);

this.windowHandle.close();
this.emitAuthStatus(true);
Expand All @@ -97,6 +108,8 @@ export class AuthService {
}

public doLogout() {
localStorage.removeItem('access_token');
localStorage.removeItem('expires');
this.authenticated = false;
this.expiresTimerId = null;
this.expires = 0;
Expand All @@ -122,15 +135,15 @@ export class AuthService {
}

public getSession() {
return {authenticated: this.authenticated, token: this.token, expires: this.expires};
return { authenticated: this.authenticated, token: this.token, expires: this.expires };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that the formatting stay consistent with the rest of the code; no spaces inside the braces.

}

private fetchUserInfo() {
if (this.token != null) {
var headers = new Headers();
headers.append('Authorization', `Bearer ${this.token}`);
//noinspection TypeScriptUnresolvedFunction
this.http.get(this.oAuthUserUrl, {headers: headers})
this.http.get(this.oAuthUserUrl, { headers: headers })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that the formatting stay consistent with the rest of the code; no spaces inside the braces.

.map(res => res.json())
.subscribe(info => {
this.userInfo = info;
Expand All @@ -148,15 +161,16 @@ export class AuthService {
return this.userInfo ? this.userInfo[this.oAuthUserNameField] : null;
}

private startExpiresTimer(seconds: number) {
private startExpiresTimer(milis: number) {
milis = milis - Date.now();
if (this.expiresTimerId != null) {
clearTimeout(this.expiresTimerId);
}
this.expiresTimerId = setTimeout(() => {
console.log('Session has expired');
this.doLogout();
}, seconds * 1000); // seconds * 1000
console.log('Token expiration timer set for', seconds, "seconds");
}, milis);
console.log('Token expiration timer set for', milis / 1000, "seconds");
}

public subscribe(onNext: (value: any) => void, onThrow?: (exception: any) => void, onReturn?: () => void) {
Expand Down