Added better routing and auto logout on login

Note that this is fully working auth login now
This commit is contained in:
piratefinn 2017-06-08 16:05:01 +01:00
parent c7810de2b6
commit 845b166364
5 changed files with 27 additions and 9 deletions

View file

@ -15,7 +15,7 @@ export class AuthGuard implements CanActivate {
// not logged in so redirect to login page with the return url // not logged in so redirect to login page with the return url
console.log('no session key found') console.log('no session key found')
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }}); this.router.navigate(['/pages/login'], { queryParams: { returnUrl: state.url }});
return false; return false;
} }
} }

View file

@ -15,7 +15,7 @@ import { BreadcrumbsComponent } from './shared/breadcrumb.component';
// Routing & Guard Module // Routing & Guard Module
import { AppRoutingModule } from './app.routing'; import { AppRoutingModule } from './app.routing';
import { AuthGuard } from './_guards/index'; import { AuthGuard } from './_guards/auth.guard';
// Layouts // Layouts
import { FullLayoutComponent } from './layouts/full-layout.component'; import { FullLayoutComponent } from './layouts/full-layout.component';

View file

@ -1,6 +1,6 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router'; import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './_guards/index'; import { AuthGuard } from './_guards/auth.guard';
// Layouts // Layouts
import { FullLayoutComponent } from './layouts/full-layout.component'; import { FullLayoutComponent } from './layouts/full-layout.component';
@ -15,7 +15,7 @@ export const routes: Routes = [
{ {
path: '', path: '',
component: FullLayoutComponent, component: FullLayoutComponent,
// canActivate: [AuthGuard], canActivate: [AuthGuard],
data: { data: {
title: 'Home' title: 'Home'
}, },

View file

@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ApiService } from '../providers/api-service'; import { ApiService } from '../providers/api-service';
import {Router } from '@angular/router'; import { Router } from '@angular/router';
@Component({ @Component({
selector: 'app-dashboard', selector: 'app-dashboard',
@ -26,6 +26,7 @@ export class FullLayoutComponent implements OnInit {
} }
ngOnInit(): void {} ngOnInit(): void {}
userlogout() { userlogout() {
console.log('logout clicked'); console.log('logout clicked');
this.api this.api
@ -33,6 +34,7 @@ export class FullLayoutComponent implements OnInit {
.subscribe( .subscribe(
result => { result => {
console.log('Logged out!'); console.log('Logged out!');
window.location.reload();
} }
); );
} }

View file

@ -1,18 +1,20 @@
import { Component } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Validators, FormBuilder, FormGroup } from '@angular/forms'; import { Validators, FormBuilder, FormGroup } from '@angular/forms';
import { Http, Response } from '@angular/http'; import { Http, Response } from '@angular/http';
import { ApiService } from '../providers/api-service'; import { ApiService } from '../providers/api-service';
import {Router } from '@angular/router'; import { Router, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
@Component({ @Component({
templateUrl: 'login.component.html', templateUrl: 'login.component.html',
providers: [ApiService] providers: [ApiService]
}) })
export class LoginComponent { export class LoginComponent implements OnInit {
signin: FormGroup; signin: FormGroup;
returnUrl: string;
constructor( constructor(
private route: ActivatedRoute,
private http: Http, private http: Http,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private router: Router, private router: Router,
@ -24,6 +26,20 @@ export class LoginComponent {
}); });
} }
ngOnInit() {
// reset login status
this.api
.logout()
.subscribe(
result => {
console.log('Logged out!');
}
);
// get return url from route parameters or default to '/'
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
onSubmit() { onSubmit() {
console.log(this.signin.value); console.log(this.signin.value);
@ -32,7 +48,7 @@ export class LoginComponent {
.subscribe( .subscribe(
result => { result => {
console.log('logged in!'); console.log('logged in!');
this.router.navigate(['/dashboard']) this.router.navigate([this.returnUrl]);
}, },
error => { error => {
console.log( error._body ); console.log( error._body );