简体   繁体   English

如何在服务中运行 function 以先获得 API 密钥?

[英]How can I run a function in a service, to get an API key before anything else?

I'm running ionic-angular framework working on an app that was started before me.我正在运行 ionic-angular 框架,该框架在我之前启动的应用程序上工作。 I need to run a function in a service to get an API key from an external server before anything else.我需要在服务中运行 function 以先从外部服务器获取 API 密钥。 Because I want to check if a user has an API key and check if their stored GUID is valid by making another request to the server which I need the API key for.因为我想检查用户是否有 API 密钥,并通过向服务器发出另一个请求来检查他们存储的 GUID 是否有效,我需要 API 密钥。 Because I'm going to check if they need to be routed to the login page or not.因为我要检查它们是否需要路由到登录页面。 When I have a route guard checking if a user is logged in my API requests aren't completed or error out because I don't have an API key yet.当我有路由保护检查用户是否登录时,我的 API 请求未完成或出错,因为我还没有 API 密钥。

If I understood your problem you're asking this: you need to check "something" to decide if the user goes to the login page or goes to another screen, at the beginning of your app, when you open it.如果我理解您的问题,您是在问这个问题:您需要检查“某事”以确定用户是在您的应用程序开始时进入登录页面还是进入另一个屏幕,当您打开它时。 And that "something" consists in making http requests.而那个“东西”包括发出 http 请求。

I will tell you how to do it, but in my experience if you need to make HTTP requests to decide to what screen redirect the user, you will get a blank page in the meanwhile.我会告诉你怎么做,但根据我的经验,如果你需要发出 HTTP 请求来决定将用户重定向到哪个屏幕,同时你会得到一个空白页面。 Depending on the connection 0.1s, 0.3s, 0.7s... But it's uggly.取决于连接 0.1s、0.3s、0.7s……但它很丑。 So the alternative would be to create a "Splash" screen with a loader circle, and use that page as the initial page.因此,另一种选择是创建一个带有加载程序圆圈的“启动”屏幕,并将该页面用作初始页面。 Then the page checks whatever and takes you to the next page.然后页面检查任何内容并将您带到下一页。

Now, answering your question: you need a "CanActivate".现在,回答您的问题:您需要一个“CanActivate”。 CanActivate is guard (a code) that is executed before accessing a route, to check if you can access that page or redirect the user to another page. CanActivate 是在访问路由之前执行的守卫(代码),以检查您是否可以访问该页面或将用户重定向到另一个页面。 This is very useful for local checks (like checking the local storage) but as I said, http requests will cause a blank page for a small time.这对于本地检查(例如检查本地存储)非常有用,但正如我所说,http 请求会在短时间内导致空白页。 Follow these steps:按着这些次序:

1 - First you need to create the CanActivate class. 1 - 首先您需要创建 CanActivate class。 That's like creating a normal Service in ionic.这就像在 ionic 中创建一个普通的服务。 Then make your service implement the CanActivate interface:然后让你的服务实现 CanActivate 接口:

import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } form 'rxjs';  // Install 'npm i rxjs' if you don't have it!

@Injectable({
  providedIn: 'root'
})
export class LoginGuard implements CanActivate { }

Then this service needs to implement a function called canActivate:那么这个服务需要实现一个名为canActivate的function:

export class LoginGuard implements CanActivate {

   constructor(private router: Router) { }

   canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : boolean|Observable<boolean> {

       return new Observable<boolean>( observer => {

           // HERE CHECK
           if (!condition_to_avoid_login) {
                // Complete the expected route, and enter the login
                observer.next(true);
                observer.complete();
           }
           else {
                // Avoid login, go somewhere else:
                observer.next(false);
                this.router.navigate("/my-other-page");
                observer.complete();
           }

       })

   }

}

2 - You need to add this Guard to your route. 2 - 您需要将此 Guard 添加到您的路线中。 In your routing file: app-routing.module.ts, add this guard to your page:在您的路由文件:app-routing.module.ts 中,将此保护添加到您的页面:

import { LoginGuard } from '...../login-guard.service';

const routes: Routes = [
   ...
   {
      path: 'login',
      loadChildren: () => import('...../login.module').then( m => m.LoginPageModule),
      canActivate: [LoginGuard]
   }
   ...
]

Now everytime the user accesses this route (/login) the LoginGuard will trigger.现在,每次用户访问此路由 (/login) 时,LoginGuard 都会触发。 There you decide if continue to the login page or redirect.在那里您决定是继续登录页面还是重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM