gpt4 book ai didi

mysql - 我应该如何将 Auth0 与 RESTful api 一起使用?

转载 作者:行者123 更新时间:2023-11-29 20:00:21 25 4
gpt4 key购买 nike

我正在考虑使用 Auth0 在我的 Nodejs API 上登录我的用户。

我正在使用 MySQL 数据库来登录他们,我还想使用 Facebook,以便他们可以注册和登录。

我对回调的概念有疑问,因为我的 API 不应该通过浏览器访问。只有网络应用程序或移动应用程序才能访问它。我如何在我的移动应用程序上实现登录/登录表单输入的处理才能使用我应该使用 Auth0 的 API?

谢谢您的回答。

最佳答案

Auth0 在免费帐户上附带一个数据库。当您将登录注册小部件添加到您的应用程序并且用户注册时,它会将它们添加到您的 auth0 帐户的数据库中。

您可以查看有关进程的信息 here

我所做的是使用 auth0 小部件对用户进行身份验证。这允许 auth0 处理加密和安全性。然后,当用户登录时,我在响应中请求配置文件。通常,这至少会给我提供基本信息,例如电子邮件地址。我使用电子邮件地址作为唯一 key 创建自己的数据库,这使我能够在用户登录时向他们提供正确的数据。

这是我的 auth0 服务的示例,它使用小部件并在响应中请求用户的配置文件,然后将其存储到本地存储。

import { Injectable }                      from '@angular/core';
import { tokenNotExpired, JwtHelper } from 'angular2-jwt';
import { Router } from '@angular/router';
import { myConfig } from './auth.config';

declare var Auth0Lock: any;

var options = {
theme: {
logo: '/img/logo.png',
primaryColor: '#779476'
},
languageDictionary: {
emailInputPlaceholder: "email@example.com",
title: "Login or SignUp"
},
};

@Injectable()
export class Auth {
lock = new Auth0Lock(myConfig.clientID, myConfig.domain, options, {});
userProfile: Object;
constructor(private router: Router) {
this.userProfile = JSON.parse(localStorage.getItem('profile'));
this.lock.on('authenticated', (authResult: any) => {
localStorage.setItem('access_token', authResult.idToken);
this.lock.getProfile(authResult.idToken, (error: any, profile: any) => {
if (error) {
console.log(error);
return;
}
localStorage.setItem('profile', JSON.stringify(profile));
this.userProfile = profile;
this.router.navigateByUrl('/overview');
});
this.lock.hide();
});
}

public login() {
this.lock.show();
}

private get accessToken(): string {
return localStorage.getItem('access_token');
}

public authenticated(): boolean {
try {
var jwtHelper: JwtHelper = new JwtHelper();
var token = this.accessToken;
if (jwtHelper.isTokenExpired(token))
return false;
return true;
}
catch (err) {
return false;
}
}

public logout() {
localStorage.removeItem('profile');
localStorage.removeItem('access_token');
this.userProfile = undefined;
this.router.navigateByUrl('/home');
};
}

关于mysql - 我应该如何将 Auth0 与 RESTful api 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com