use session data

You can use session.*,Captcha,PasswordValidateJwtMake, JwtValidate and other processes to implement custom user login and user identity authentication.

The session data set when logging in can be used in data streams, JS-written processes and data tables.

Account password login

Account and password login process:

  1. On the login page, the user fills in the account name, verification code and password, and clicks the button to submit to the specified API.

  2. The API receives the account name and password information, transfers it to the designated process, verifies the password, and issues a JWT token.

process password verification logic:

  1. Call the CaptchaValidate process to verify the captcha.

  2. Call the data model process to query the user data record through the account name, and obtain the encrypted stored password information.

  3. Call the PasswordValidate process to verify whether the user fills in the correct password.

  4. Issue a JWT token and set session information.

Step 1: User Data Model

Write user.tab.json and place it in the application tables directory.

View source code

Create data table & add default user:

yao migrate -n user

Step 2: Login handler

Write password.flow.json and token.flow.json in the application flows/login/ directory. Implement password verification logic and JWT token issuance logic respectively.

View source code
Tip: Combine password verification with signature tokenSplit into two processes, the token issuance process can be reused in scenarios such as OAuth third-party login, SMS verification code login, etc.

Run process Debug:

Tip: To facilitate debugging, you can add a verification code verification node after the login logic is successfully debugged.
Note: Due to some historical reasons, in the login submission information of the built-in login interface of yao, the user field name is fixed asmobile, will be allowed to be defined in the application description file in subsequent versions.
yao run flows.login.password '::{"mobile":"zhang@yaoapps.com", "password": "5MCIXQYrR"}'

Step 3: Login API

Write the interface description file user.http.json, add two interfaces /captcha and /login/password, which are used for verification code picture and username and password login interface respectively, and place them in the api directory of the application .

Description file content:

View source code

Interface debugging:

Start the service:

yao start

Read verification code interface:

curl http://127.0.0.1:5099/xiang/api/user/captcha
Tip: Set the environment variable to enable debug mode. After making a request, you can view the verification code id in the service log and code, for debugging.

Login interface:

curl -X POST http://127.0.0.1:5099/xiang/api/user/login/password \
-H 'Content-Type: application/json' \
-d '{"mobile":"zhang@yaoapps.com", "password": "5MCIXQYrR", "captcha":{"id":1024, "code":"xv98"}}'

Step 4: Application Description

Edit app.json to set up the user login API. The login interface route is /xiang/login/user/:is , is is a custom variable, which is submitted to the login interface along with the login form to identify the source of the user, and is generally used in multi-tenant systems.

{
"name": "Elephant Biography",
"short": "Elephant Biography",
"description": "Elephant app background",
"option": {
"nav_user": "xiang.user",
"nav_menu": "menu",
"hide_user": true,
"hide_menu": true,
"login": {
"password": {
"captcha": "/api/xiang/user/captcha",
"login": "/api/user/login/password"
}
}
}
}

use session data

The session data set by the user's successful login can be used in data streams, JS scripts and data tables.

Use in data flow

Write inspect.flow.json and place it in the application flows/user/ directory.

{
"label": "Current user information",
"version": "1.0.0",
"description": "Current user information",
"nodes": [
{
"name": "Session",
"process": "session.Get",
"args": ["user"]
}
],
"output": "{{$res.session}}"
}

Run Debug:

Start the service and enable debug mode:

yao start --debug

Create a new command console:

Note: To enable the run@ command, you need to enable debug mode or set the environment variableXIANG_REMOTE_RUNSet to IP whitelist. For example: XIANG_REMOTE_RUN=127.0.0.1
yao run@127.0.0.1:5099 login
yao run@127.0.0.1:5099 flows.user.inspect

Use in JS scripts

Write user.js and place it in the application scripts directory.

function Inspect() {
return Process("session.Get", "user");
}
yao run@127.0.0.1:5099 login
yao run@127.0.0.1:5099 scripts.user.Inspect

Use in data tables

Session variables can be referenced directly in the data table apis.*.default.

View source code

use session data

You can use session.*,Captcha,PasswordValidateJwtMake, JwtValidate and other processes to implement custom user login and user identity authentication.

The session data set when logging in can be used in data streams, JS-written processes and data tables.

Account password login

Account and password login process:

  1. On the login page, the user fills in the account name, verification code and password, and clicks the button to submit to the specified API.

  2. The API receives the account name and password information, transfers it to the designated process, verifies the password, and issues a JWT token.

process password verification logic:

  1. Call the CaptchaValidate process to verify the captcha.

  2. Call the data model process to query the user data record through the account name, and obtain the encrypted stored password information.

  3. Call the PasswordValidate process to verify whether the user fills in the correct password.

  4. Issue a JWT token and set session information.

Step 1: User Data Model

Write user.tab.json and place it in the application tables directory.

View source code

Create data table & add default user:

yao migrate -n user

Step 2: Login handler

Write password.flow.json and token.flow.json in the application flows/login/ directory. Implement password verification logic and JWT token issuance logic respectively.

View source code
Tip: Combine password verification with signature tokenSplit into two processes, the token issuance process can be reused in scenarios such as OAuth third-party login, SMS verification code login, etc.

Run process Debug:

Tip: To facilitate debugging, you can add a verification code verification node after the login logic is successfully debugged.
Note: Due to some historical reasons, in the login submission information of the built-in login interface of yao, the user field name is fixed asmobile, will be allowed to be defined in the application description file in subsequent versions.
yao run flows.login.password '::{"mobile":"zhang@yaoapps.com", "password": "5MCIXQYrR"}'

Step 3: Login API

Write the interface description file user.http.json, add two interfaces /captcha and /login/password, which are used for verification code picture and username and password login interface respectively, and place them in the api directory of the application .

Description file content:

View source code

Interface debugging:

Start the service:

yao start

Read verification code interface:

curl http://127.0.0.1:5099/xiang/api/user/captcha
Tip: Set the environment variable to enable debug mode. After making a request, you can view the verification code id in the service log and code, for debugging.

Login interface:

curl -X POST http://127.0.0.1:5099/xiang/api/user/login/password \
-H 'Content-Type: application/json' \
-d '{"mobile":"zhang@yaoapps.com", "password": "5MCIXQYrR", "captcha":{"id":1024, "code":"xv98"}}'

Step 4: Application Description

Edit app.json to set up the user login API. The login interface route is /xiang/login/user/:is , is is a custom variable, which is submitted to the login interface along with the login form to identify the source of the user, and is generally used in multi-tenant systems.

{
"name": "Elephant Biography",
"short": "Elephant Biography",
"description": "Elephant app background",
"option": {
"nav_user": "xiang.user",
"nav_menu": "menu",
"hide_user": true,
"hide_menu": true,
"login": {
"password": {
"captcha": "/api/xiang/user/captcha",
"login": "/api/user/login/password"
}
}
}
}

use session data

The session data set by the user's successful login can be used in data streams, JS scripts and data tables.

Use in data flow

Write inspect.flow.json and place it in the application flows/user/ directory.

{
"label": "Current user information",
"version": "1.0.0",
"description": "Current user information",
"nodes": [
{
"name": "Session",
"process": "session.Get",
"args": ["user"]
}
],
"output": "{{$res.session}}"
}

Run Debug:

Start the service and enable debug mode:

yao start --debug

Create a new command console:

Note: To enable the run@ command, you need to enable debug mode or set the environment variableXIANG_REMOTE_RUNSet to IP whitelist. For example: XIANG_REMOTE_RUN=127.0.0.1
yao run@127.0.0.1:5099 login
yao run@127.0.0.1:5099 flows.user.inspect

Use in JS scripts

Write user.js and place it in the application scripts directory.

function Inspect() {
return Process("session.Get", "user");
}
yao run@127.0.0.1:5099 login
yao run@127.0.0.1:5099 scripts.user.Inspect

Use in data tables

Session variables can be referenced directly in the data table apis.*.default.

View source code