将MongoDB Atlas上的用户无缝迁移到Auth0
首先
我们将介绍如何将在MongoDB Atlas上创建的用户数据存储与Auth0连接,以迁移现有用户到Auth0的步骤。本文是根据原文制作而成,原文请参考此处。验证需要先安装Node.js、NPM,以及已完成获取Auth0免费账户和创建租户的前提。如果您尚未完成,请在参考本文之前先阅读此处的文章进行准备。
验证环境
端末のOS : macOS Mojave 10.14.5
MongoDB Atlas : Version 4.0.9
Mongo Shell : Version 4.0.9
Node.js : 10.15.3
npm : 6.4.1
操作过程
蒙高侧
我们将在终端上安装Mongo Shell。
$ brew install mongodb/brew/mongodb-community-shell
在创建管理员用户时输入设置的密码以进行连接。
$ mongo "mongodb+srv://cluster0-rt8x2.mongodb.net/test" --username auth0-custom-db-user 15:59:03
MongoDB shell version v4.0.9
Enter password:
connecting to: mongodb://cluster0-shard-00-00-rt8x2.mongodb.net.:27017,cluster0-shard-00-01-rt8x2.mongodb.net.:27017,cluster0-shard-00-02-rt8x2.mongodb.net.:27017/test?authSource=admin&gssapiServiceName=mongodb&replicaSet=Cluster0-shard-0&ssl=true
2019-06-03T15:59:16.760+0900 I NETWORK [js] Starting new replica set monitor for Cluster0-shard-0/cluster0-shard-00-00-rt8x2.mongodb.net.:27017,cluster0-shard-00-01-rt8x2.mongodb.net.:27017,cluster0-shard-00-02-rt8x2.mongodb.net.:27017
2019-06-03T15:59:17.459+0900 I NETWORK [js] Successfully connected to cluster0-shard-00-00-rt8x2.mongodb.net.:27017 (1 connections now open to cluster0-shard-00-00-rt8x2.mongodb.net.:27017 with a 5 second timeout)
2019-06-03T15:59:17.459+0900 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to cluster0-shard-00-01-rt8x2.mongodb.net.:27017 (1 connections now open to cluster0-shard-00-01-rt8x2.mongodb.net.:27017 with a 5 second timeout)
2019-06-03T15:59:17.621+0900 I NETWORK [js] changing hosts to Cluster0-shard-0/cluster0-shard-00-00-rt8x2.mongodb.net:27017,cluster0-shard-00-01-rt8x2.mongodb.net:27017,cluster0-shard-00-02-rt8x2.mongodb.net:27017 from Cluster0-shard-0/cluster0-shard-00-00-rt8x2.mongodb.net.:27017,cluster0-shard-00-01-rt8x2.mongodb.net.:27017,cluster0-shard-00-02-rt8x2.mongodb.net.:27017
2019-06-03T15:59:18.299+0900 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to cluster0-shard-00-00-rt8x2.mongodb.net:27017 (1 connections now open to cluster0-shard-00-00-rt8x2.mongodb.net:27017 with a 5 second timeout)
2019-06-03T15:59:18.299+0900 I NETWORK [js] Successfully connected to cluster0-shard-00-01-rt8x2.mongodb.net:27017 (1 connections now open to cluster0-shard-00-01-rt8x2.mongodb.net:27017 with a 5 second timeout)
2019-06-03T15:59:20.101+0900 I NETWORK [ReplicaSetMonitor-TaskExecutor] Successfully connected to cluster0-shard-00-02-rt8x2.mongodb.net:27017 (1 connections now open to cluster0-shard-00-02-rt8x2.mongodb.net:27017 with a 5 second timeout)
Implicit session: session { "id" : UUID("fc24b722-7351-441a-b772-a9143054f0f8") }
MongoDB server version: 4.0.9
Error while trying to show server startup warnings: user is not allowed to do action [getLog] on [admin.]
MongoDB Enterprise Cluster0-shard-0:PRIMARY>
我将在数据库名为auth0-integration、集合名为users的地方分别创建它们。
> use auth0-integration;
> db.createCollection('users');
我会确认是否已创建了 Collection。
> show collections
users
输入样本登录用户。
> db.users.insert(
[
{ username: "a.user@spam4.me", password: "som3Passw0rd!" },
{ username: "another.user@spam4.me", password: "newPassw0rd!" },
{ username: "yet.another.user@spam4.me", password: "an0th3rPwd!" }
]
)
我将确认是否成功投入。
> db.users.find();
{ "_id" : ObjectId("5cf4c9c1e6e0be4e6822b4c8"), "username" : "a.user@spam4.me", "password" : "som3Passw0rd!" }
{ "_id" : ObjectId("5cf4c9c1e6e0be4e6822b4c9"), "username" : "another.user@spam4.me", "password" : "newPassw0rd!" }
{ "_id" : ObjectId("5cf4c9c1e6e0be4e6822b4ca"), "username" : "yet.another.user@spam4.me", "password" : "an0th3rPwd!" }
只需要一个选项的中文释义:Auth0方面。
修改登录脚本并点击“保存”按钮。
– 由于Auth0仅支持Mongo Library 3.1.4(截至2019年6月),请明确指定版本。
– dbHost的格式为“xxxx.mongodb.net”。
– 本文中为了验证目的,密码以明文指定。请在生产环境中添加加密处理。
function login(email, password, callback) {
const {MongoClient} = require("mongodb@3.1.4");
const dbUser = "auth0-customdb-user";
const dbPwd = "xxxx";
const dbHost = "cluster0-kft70.mongodb.net";
const dbName = "auth0-integration";
const usersCollection = "users";
const uri = `mongodb+srv://${dbUser}:${dbPwd}@${dbHost}/test?retryWrites=true`;
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
if (err) return callback(err);
const collection = client.db(dbName).collection(usersCollection);
collection.findOne({username: email, password}, (err, user) => {
if (err) return callback(err);
if (!user) return callback("User not found.");
const profile = {
...user,
user_id: user.username
};
callback(null, profile);
client.close();
});
});
}
点击”自定义数据库”选项卡,在”数据库操作脚本”中点击”获取用户”以修正脚本,然后点击”保存”按钮。
function getByEmail(email, callback) {
const {MongoClient} = require("mongodb@3.1.4");
const dbUser = "auth0-customdb-user";
const dbPwd = "xxxx";
const dbHost = "cluster0-kft70.mongodb.net";
const dbName = "auth0-integration";
const usersCollection = "users";
const uri = `mongodb+srv://${dbUser}:${dbPwd}@${dbHost}/test?retryWrites=true`;
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
if (err) return callback(err);
const collection = client.db(dbName).collection(usersCollection);
collection.findOne({username: email}, (err, user) => {
if (err) return callback(err);
if (!user) return callback("User not found.");
delete user.password;
const profile = {
...user,
user_id: user.username
};
callback(null, profile);
client.close();
});
});
}
确认动作
请将auth0-react-samples/01-Login/src/Auth/auth0-variables.js.example复制到同一目录下,并创建一个名为auth0-variables.js的文件。
$ cp auth0-variables.js.example auth0-variables.js
请打开auth0-variables.js文件并指定domain和clientId。您可以在”Settings”选项卡中查找到domain和clientId信息。
export const AUTH_CONFIG = {
domain: 'mokomoko.auth0.com',
clientId: 'mB44RrIUvRNRpv0VQH1OOnlWicd4xHDP',
callbackUrl: 'http://localhost:3000/callback'
}
按照Readme的指示运行示例应用程序。
$ pwd
/tmp/auth0-react-samples/01-Login
$ npm install
$ npm start
最后
在本文中,我们介绍了将现有用户迁移到Auth0的步骤,但Auth0也可以继续使用已经使用的外部用户数据存储。在这种情况下,您可以将”Import Users to Auth0″翻转开关关闭,并在Auth0仪表板上设置除登录之外的脚本(例如用户创建、删除和密码更改)。