Skip to content

无密码

¥Passwordless

无密码包允许你为用户创建登录名,而无需用户提供密码。注册或登录后,会向用户的电子邮件发送电子邮件,其中包含用于确认登录的代码和直接登录的链接。由于用户正在回复电子邮件,因此它还将验证电子邮件。

¥Passwordless package allows you to create a login for users without the need for user to provide password. Upon registering or login an email is sent to the user's email with a code to enter to confirm login and a link to login directly. Since the user is responding to the email it will also verify the email.

无密码流程的第一步是用户注册或向其电子邮件地址请求令牌。你可以使用以下方法执行此操作:

Accounts.requestLoginTokenForUser

Client only

Summary:

Request a login token.

Arguments:

Source code
NameTypeDescriptionRequired
optionsObjectYes
callbackfunction

Optional callback. Called with no arguments on success, or with a single Error argument on failure.

No

Options:

NameTypeDescriptionRequired
selectorString

The email address to get a token for or username or a mongo selector.

No
userDataString

When creating a user use this data if selector produces no result.

No
optionsObject

For example userCreationDisabled.

No
js
import { Accounts } from "meteor/accounts-base";


const result = Accounts.requestLoginTokenForUser();
  options,
() => {}, // this param is optional
);

¥The first step to in the passwordless process is for the user to sign-up or request a token to their email address. You can do that with the following:

Accounts.requestLoginTokenForUser

Client only

Summary:

Request a login token.

Arguments:

Source code
NameTypeDescriptionRequired
optionsObjectYes
callbackfunction

Optional callback. Called with no arguments on success, or with a single Error argument on failure.

No

Options:

NameTypeDescriptionRequired
selectorString

The email address to get a token for or username or a mongo selector.

No
userDataString

When creating a user use this data if selector produces no result.

No
optionsObject

For example userCreationDisabled.

No
js
import { Accounts } from "meteor/accounts-base";


const result = Accounts.requestLoginTokenForUser();
  options,
() => {}, // this param is optional
);

如果用户正在注册,你可以像在 Accounts.createUser 中一样传入 userData 对象。

¥If the user is signing up you can pass in the userData object like in Accounts.createUser.

Meteor.passwordlessLoginWithToken

Client only

Summary:

Log the user in with a one time token.

Arguments:

Source code
NameTypeDescriptionRequired
selectorObject or String

Username, email or custom selector to identify the user.

Yes
tokenString

one time token generated by the server

Yes
callbackfunction

Optional callback. Called with no arguments on success, or with a single Error argument on failure.

No
js
import { Meteor } from "meteor/meteor";


const result = Meteor.passwordlessLoginWithToken();
  selector,
"token",

() => {}, // this param is optional
);

The second step in the passwordless flow. Like all the other loginWith functions call this method to login the user with the token they have inputted.

Accounts.sendLoginTokenEmail

Server only

Summary:

Send an email with a link the user can use to login with token.

Arguments:

Source code
NameTypeDescriptionRequired
optionsObjectYes

Options:

NameTypeDescriptionRequired
userIdString

The id of the user to send email to.

No
sequenceString

The token to be provided

No
emailString

Which address of the user's to send the email to.

No
extraObject

Optional. Extra properties

No
js
import { Accounts } from "meteor/accounts-base";

/** @returns Object */
const result = Accounts.sendLoginTokenEmail();
  options
);

Use this function if you want to manually send the email to users to login with token from the server. Do note that you will need to create the token/sequence and save it in the DB yourself. This is good if you want to change how the tokens look or are generated, but unless you are sure of what you are doing we don't recommend it.

设置选项

你可以使用服务器中的 Accounts.config 函数更改此包上的某些设置:

¥You can use the function Accounts.config in the server to change some settings on this package:

  • tokenSequenceLength:使用 Accounts.config({tokenSequenceLength: _Number_}) 生成令牌序列的大小。默认值为 6。

    ¥tokenSequenceLength: use Accounts.config({tokenSequenceLength: _Number_}) to the size of the token sequence generated. The default is 6.

  • loginTokenExpirationHours:使用 Accounts.config({loginTokenExpirationHours: _Number_}) 设置发送的令牌有效的时间量。由于它只是一个数字,因此你可以使用例如 0.5 使令牌仅在半小时内有效。默认值为 1 小时。

    ¥loginTokenExpirationHours: use Accounts.config({loginTokenExpirationHours: _Number_}) to set the amount of time a token sent is valid. As it's just a number, you can use, for example, 0.5 to make the token valid for just half hour. The default is 1 hour.

电子邮件模板

accounts-passwordless 带来了新的模板,你可以编辑这些模板以更改向用户发送代码的电子邮件的外观。电子邮件模板名为 sendLoginToken,除了 userurl 之外,模板还接收带有 sequence 的数据对象,即用户的代码。

¥accounts-passwordless brings new templates that you can edit to change the look of emails which send code to users. The email template is named sendLoginToken and beside user and url, the templates also receive a data object with sequence which is the user's code.

javascript
sendLoginToken: {
  text: (user, url, { sequence }) => {
    /* text template */
  };
}

为此包启用 2FA

你可以使用包 accounts-2fa 将 2FA 添加到你的登录流程中。你可以找到一个显示它看起来像 此处 的示例。

¥You can add 2FA to your login flow by using the package accounts-2fa. You can find an example showing how this would look like here.