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
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;
}
}

View file

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

View file

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

View file

@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { ApiService } from '../providers/api-service';
import {Router } from '@angular/router';
import { Router } from '@angular/router';
@Component({
selector: 'app-dashboard',
@ -26,6 +26,7 @@ export class FullLayoutComponent implements OnInit {
}
ngOnInit(): void {}
userlogout() {
console.log('logout clicked');
this.api
@ -33,6 +34,7 @@ export class FullLayoutComponent implements OnInit {
.subscribe(
result => {
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 { Http, Response } from '@angular/http';
import { ApiService } from '../providers/api-service';
import {Router } from '@angular/router';
import { Router, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/map';
@Component({
templateUrl: 'login.component.html',
providers: [ApiService]
})
export class LoginComponent {
export class LoginComponent implements OnInit {
signin: FormGroup;
returnUrl: string;
constructor(
private route: ActivatedRoute,
private http: Http,
private formBuilder: FormBuilder,
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() {
console.log(this.signin.value);
@ -32,7 +48,7 @@ export class LoginComponent {
.subscribe(
result => {
console.log('logged in!');
this.router.navigate(['/dashboard'])
this.router.navigate([this.returnUrl]);
},
error => {
console.log( error._body );