Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Injectable()
- export class AuthGuard implements CanActivate {
- constructor(
- private router: Router,
- @Inject(PLATFORM_ID) private platformId: Object,
- private authService: AuthenticationService
- ) { }
- canActivate() {
- if (isPlatformBrowser(this.platformId)) {
- if(this.authService.auth2) {
- if (this.authService.loggedIn()) {
- return true;
- } else {
- this.router.navigate(['/login']);
- return false;
- }
- }
- }
- }
- }
- @Injectable()
- export class AuthenticationService {
- token = new Subject<String>();
- auth2;
- auth2access;
- constructor(
- private router: Router,
- @Inject(PLATFORM_ID) private platformId: Object
- ) {
- if (isPlatformBrowser(this.platformId)) {
- try {
- this.token.next(sessionStorage.getItem('token'));
- gapi.load('auth2', () => {
- this.auth2 = gapi.auth2.init({
- client_id: '*.apps.googleusercontent.com'
- }).then( () =>
- {
- //var googleAuthTest = gapi.auth2.getAuthInstance();
- console.log(gapi.auth2.getAuthInstance().isSignedIn.get());
- if(gapi.auth2.getAuthInstance().isSignedIn.get() == false) {
- this.revoke();
- }else{
- this.auth2access = gapi.auth2.getAuthInstance();
- }
- });
- });
- } catch (ex) { }
- }
- }
- public backendAuth(id_token ,action) {
- var result = new BehaviorSubject<string>('');
- var xhr = new XMLHttpRequest();
- xhr.open('POST', 'https://callum.tech/auth/verify.php');
- xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- xhr.onload = function() {
- result.next(xhr.responseText);
- };
- xhr.send('id_token=' + id_token + "&action=" + action);
- return result.map(x => x);
- }
- public loggedIn(): boolean {
- if (isPlatformBrowser(this.platformId)) {
- if(this.auth2) {
- return gapi.auth2.getAuthInstance().isSignedIn.get();
- }
- }
- /*if (isPlatformBrowser(this.platformId)) {
- if(this.auth2) {
- return gapi.auth2.getAuthInstance().isSignedIn.get();
- }
- }*/
- }
- public revoke() {
- if (isPlatformBrowser(this.platformId)) {
- gapi.auth2.getAuthInstance().signOut();
- }
- }
- }
Add Comment
Please, Sign In to add comment