根据用户的登录状态加载组件而不更改URL

时间:2018-01-09 04:56:00

标签: angular

我正在使用Angular。在我的DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.bitbucket.org send: b'GET /2.0/repositories/Anexampleuser/repo/downloads/keys.gz HTTP/1.1\r\nHost: api.bitbucket.org\r\nAccept-Encoding: identity\r\nauthorization: Basic YnJvZmV3ZmVmd2VmZXdlZjpFS0FYc1drZHQ1SDZ5SkVtdGV4Tg==\r\n\r\n' reply: 'HTTP/1.1 302 Found\r\n' DEBUG:urllib3.connectionpool:https://api.bitbucket.org:443 "GET/2.0/repositories/Anexampleuser/repo/downloads/keys.gz HTTP/1.1" 302 0 DEBUG:urllib3.util.retry:Incremented Retry for (url='https://api.bitbucket.org/2.0/repositories/Anexampleuser/repo/downloads/keys.gz'): Retry(total=2, connect=None, read=None, redirect=None, status=None) INFO:urllib3.poolmanager:Redirecting https://api.bitbucket.org/2.0/repositories/Anexampleuser/repo/downloads/keys.gz -> https://bbuseruploads.s3.amazonaws.com/a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/3fc0be6d-ca69-42d3-9711-fbb5cfd2bc38/keys.gz?Signature=ZQxeUTvYC3Q%2Fo1aaS1CSuzyit0Q%3D&Expires=1515976464&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=n.ymY11KRkq36Xozy25aChvfUT.YzTf5&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22 DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bbuseruploads.s3.amazonaws.com header: Server header: Vary header: Content-Type header: X-OAuth-Scopes header: Strict-Transport-Security header: Date header: Location header: X-Served-By header: ETag header: X-Static-Version header: X-Content-Type-Options header: X-Accepted-OAuth-Scopes header: X-Credential-Type header: X-Render-Time header: Connection header: X-Request-Count header: X-Frame-Options header: X-Version header: Content-Length send: b'GET /a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/3fc0be6d-ca69-42d3-9711-fbb5cfd2bc38/keys.gz?Signature=ZQxeUTvYC3Q%2Fo1aaS1CSuzyit0Q%3D&Expires=1515976464&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=n.ymY11KRkq36Xozy25aChvfUT.YzTf5&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22 HTTP/1.1\r\nHost: bbuseruploads.s3.amazonaws.com\r\nAccept-Encoding: identity\r\nauthorization: Basic YnJvZmV3ZmVmd2VmZXdlZjpFS0FYc1drZHQ1SDZ5SkVtdGV4Tg==\r\n\r\n' reply: 'HTTP/1.1 400 Bad Request\r\n' 中,如果用户未登录,我想显示登录页面。

我有身份验证服务

AppComponent

app.component.ts

export class AuthenticationService {

    constructor() {}

    logout() {
        // logs out user
    }

    login(user) {
        //logs in user           

    }

    checkCredentials() {
        //checks if logged in 
    }
}  

login.component

import { NgModule, Component } from '@angular/core';
// import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

@NgModule({

})
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

}

如果用户未登录,则可以在app.component中为主页加载import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit { constructor() { } ngOnInit() { } } ,如果用户登录而未登录到其他网址,则LoginComponent可以执行此操作?< / p>

1 个答案:

答案 0 :(得分:0)

您可以将您的令牌存储到本地存储中,并根据该令牌的到期日期,您可以检查令牌的有效性,并根据您可以重定向到登录页面或仪表板页面, 您可以在Guards中检查有效性逻辑。

如果您想要完整示例,请参阅此链接: http://jasonwatmore.com/post/2016/09/29/angular-2-user-registration-and-login-example-tutorial

并且auth.guard.ts正在完成你需要的工作