Permalink
Browse files

redirecting to home page if login returns a token

  • Loading branch information...
ehelin committed Sep 23, 2018
1 parent 05fada9 commit feb4719ff476da197a5a9f9f3528bfeda764f8cf
@@ -26,18 +26,14 @@ import { TestComponent } from './components/test/test.component';
CommonModule,
HttpModule,
FormsModule,
RouterModule.forRoot([
//{ path: '', redirectTo: 'login', pathMatch: 'full' },
//{ path: '**', redirectTo: 'login' }
RouterModule.forRoot([
{ path: '', redirectTo: 'login', pathMatch: 'full' },
//{ path: 'home', component: HomeComponent },
//{ path: 'test', component: TestComponent },
//{ path: 'counter', component: CounterComponent },
//{ path: 'fetch-data', component: FetchDataComponent },
{ path: 'home', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: '**', redirectTo: 'login' }
])
]
],
exports: [RouterModule]
})
export class AppModuleShared {
}
@@ -1,16 +1 @@
<h1>Hello, world!</h1>
<p>Welcome to your new single-page application, built with:</p>
<ul>
<li><a href='https://get.asp.net/'>ASP.NET Core</a> and <a href='https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx'>C#</a> for cross-platform server-side code</li>
<li><a href='https://angular.io/'>Angular</a> and <a href='http://www.typescriptlang.org/'>TypeScript</a> for client-side code</li>
<li><a href='https://webpack.github.io/'>Webpack</a> for building and bundling client-side resources</li>
<li><a href='http://getbootstrap.com/'>Bootstrap</a> for layout and styling</li>
</ul>
<p>To help you get started, we've also set up:</p>
<ul>
<li><strong>Client-side navigation</strong>. For example, click <em>Counter</em> then <em>Back</em> to return here.</li>
<li><strong>Server-side prerendering</strong>. For faster initial loading and improved SEO, your Angular app is prerendered on the server. The resulting HTML is then transferred to the browser where a client-side copy of the app takes over.</li>
<li><strong>Webpack dev middleware</strong>. In development mode, there's no need to run the <code>webpack</code> build tool. Your client-side resources are dynamically built on demand. Updates are available as soon as you modify any file.</li>
<li><strong>Hot module replacement</strong>. In development mode, you don't even need to reload the page after making most changes. Within seconds of saving changes to files, your Angular app will be rebuilt and a new instance injected into the page.</li>
<li><strong>Efficient production builds</strong>. In production mode, development-time features are disabled, and the <code>webpack</code> build tool produces minified static CSS and JavaScript files.</li>
</ul>
<p>main.html</p>
@@ -1,11 +1,6 @@
import { Component, Inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Http, URLSearchParams } from '@angular/http';
//import { HttpHeaders } from '@angular/common/http';
//import { HTTP_INTERCEPTORS } from '@angular/common';
//import { HttpClientModule } from '@angular/common/http'; // replaces previous Http service
import { RequestOptionsArgs, RequestOptions } from '@angular/http';
import { HttpModule } from '@angular/http';
import { Component, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { Http, URLSearchParams } from '@angular/http';
@Component({
selector: 'login',
@@ -14,14 +9,20 @@ import { HttpModule } from '@angular/http';
export class LoginComponent {
private http: any;
private baseUrl: string;
private router: Router;
constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {
ngOnInit() {
this.router.navigate([''])
}
constructor(http: Http, @Inject('BASE_URL') baseUrl: string, router: Router) {
this.http = http;;
this.baseUrl = baseUrl;
this.router = router;
}
public Login(userName: string, password: string) {
this.post(userName, password);
this.post(userName, password, this.router);
}
public Cancel() {
@@ -30,7 +31,7 @@ export class LoginComponent {
// TODO - move to an interface/class provider
// TODO - generacize to take an array of argments
private post(userName: string, password: string) {
private post(userName: string, password: string, router: Router) {
var subUrl = '/Home/Login';
var url = this.baseUrl + subUrl;
let urlSearchParams = new URLSearchParams();
@@ -40,14 +41,15 @@ export class LoginComponent {
return this.http.post(url, urlSearchParams)
.subscribe(
function (data: any) {
//Why does the response have no token?
alert('data: ' + data._body);
//alert('data: ' + data.json());
//alert(JSON.stringify(data));
},
function (error: any) {
alert('error: ' + error);
});
function (data: any) {
if (data && data._body && data._body.length > 0) {
return router.navigate(['home'])
}
alert('Username and/or password is not correct');
},
function (error: any) {
alert('error: ' + error);
});
}
}

0 comments on commit feb4719

Please sign in to comment.