Tips Amazon-cognito-identity-js tutorial

angryduck643

New member
[TIẾNG VIỆT]:
## Amazon nhận dạng nhận dạng JS Hướng dẫn

Trong hướng dẫn này, bạn sẽ tìm hiểu cách sử dụng thư viện JS nhận dạng Amazon Cognito để đăng nhập người dùng vào ứng dụng web của bạn.Bạn cũng sẽ tìm hiểu cách quản lý các phiên người dùng và mã thông báo truy cập.

### Điều kiện tiên quyết

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Một trình duyệt web
* Nhóm nhận dạng Amazon Cognito
* Nhóm người dùng Amazon Cognito
* ID khách hàng của Amazon Cognito và bí mật

### Bắt đầu

Để bắt đầu, bạn cần tạo một nhóm nhận dạng Amazon Cognito và nhóm người dùng.Bạn có thể làm điều này bằng cách làm theo các hướng dẫn trong [tài liệu Amazon Cognito] (Amazon Cognito).

Khi bạn đã tạo một nhóm nhận dạng và nhóm người dùng, bạn cần lấy ID máy khách và bí mật cho ứng dụng của mình.Bạn có thể tìm thấy những thứ này trong phần Cài đặt ứng dụng ứng dụng ** ** của bảng điều khiển Amazon Cognito.

### Đăng nhập người dùng

Để đăng nhập vào người dùng, bạn cần sử dụng phương thức `Signin` của lớp` Amazoncognitoidentityjs`.Phương thức `Sigin` lấy các tham số sau:

* `Tên người dùng`: Tên người dùng của người dùng.
* `Mật khẩu`: Mật khẩu của người dùng.
* `Callback`: Hàm gọi lại sẽ được gọi khi yêu cầu đăng nhập hoàn tất.

Hàm `callback` sẽ được truyền một đối tượng` usersession` nếu yêu cầu đăng nhập thành công.Đối tượng `is người dùng` chứa các thuộc tính sau:

* `idtoken`: Mã thông báo ID cho người dùng.
* `accessToken`: Mã thông báo truy cập cho người dùng.
* `RefreshToken`: Mã thông báo làm mới cho người dùng.

Mã sau đây cho thấy cách đăng nhập vào người dùng:

`` `JavaScript
const amazoncognitoidentityjs = yêu cầu ('Amazon-cognito-indentity-js');

const clientId = '<Cour-client-id>';
const clientsecret = '<-client-secret>';

const nhận thức
clientid,
khách hàng mật,
});

const tên người dùng = 'tên người dùng';
mật khẩu const = 'mật khẩu';

cognitoidentity.signin (tên người dùng, mật khẩu, (err, người dùng) => {
if (err) {
Console.log (err);
} khác {
console.log (người dùng);
}
});
`` `

### Quản lý phiên người dùng

Khi người dùng đã đăng nhập, bạn có thể sử dụng phương thức `getSession` của lớp` Amazoncognitoidentityjs` để nhận phiên của người dùng.Phương thức `getessess` lấy các tham số sau:

* `accessToken`: Mã thông báo truy cập cho người dùng.

Phương thức `getSession` sẽ trả về đối tượng` useressession` nếu người dùng được đăng nhập. Đối tượng `người dùng` chứa các thuộc tính giống như đối tượng` useression` được trả về bằng phương thức `signin`.

Mã sau đây cho thấy cách nhận phiên của người dùng:

`` `JavaScript
const amazoncognitoidentityjs = yêu cầu ('Amazon-cognito-indentity-js');

const clientId = '<Cour-client-id>';
const clientsecret = '<-client-secret>';

const nhận thức
clientid,
khách hàng mật,
});

const accessToken = 'access-token';

const người dùng = cognitoidentity.getSession (accessToken);

console.log (người dùng);
`` `

Bạn cũng có thể sử dụng phương thức `RefreshSession` của lớp` Amazoncognitoidentityjs` để làm mới phiên của người dùng.Phương thức `Làm mới` lấy các tham số sau:

* `RefreshToken`: Mã thông báo làm mới cho người dùng.

Phương thức `RefreshSession` sẽ trả về một đối tượng` isSionession` mới nếu mã thông báo làm mới là hợp lệ.

Mã sau đây cho thấy cách làm mới phiên của người dùng:

`` `JavaScript

[ENGLISH]:
## Amazon Cognito Identity JS Tutorial

In this tutorial, you will learn how to use the Amazon Cognito Identity JS library to sign in users to your web application. You will also learn how to manage user sessions and access tokens.

### Prerequisites

To follow this tutorial, you will need the following:

* A web browser
* An Amazon Cognito identity pool
* An Amazon Cognito user pool
* An Amazon Cognito client ID and secret

### Getting Started

To get started, you need to create an Amazon Cognito identity pool and a user pool. You can do this by following the instructions in the [Amazon Cognito documentation](https://docs.aws.amazon.com/cognito/latest/developerguide/getting-started.html).

Once you have created an identity pool and a user pool, you need to get the client ID and secret for your application. You can find these in the **App client settings** section of the Amazon Cognito console.

### Signing In Users

To sign in a user, you need to use the `signIn` method of the `AmazonCognitoIdentityJS` class. The `signIn` method takes the following parameters:

* `username`: The username of the user.
* `password`: The password of the user.
* `callback`: A callback function that will be called when the sign-in request is complete.

The `callback` function will be passed a `UserSession` object if the sign-in request is successful. The `UserSession` object contains the following properties:

* `idToken`: The ID token for the user.
* `accessToken`: The access token for the user.
* `refreshToken`: The refresh token for the user.

The following code shows how to sign in a user:

```javascript
const AmazonCognitoIdentityJS = require('amazon-cognito-identity-js');

const clientId = '<your-client-id>';
const clientSecret = '<your-client-secret>';

const cognitoIdentity = new AmazonCognitoIdentityJS({
clientId,
clientSecret,
});

const username = 'username';
const password = 'password';

cognitoIdentity.signIn(username, password, (err, userSession) => {
if (err) {
console.log(err);
} else {
console.log(userSession);
}
});
```

### Managing User Sessions

Once a user has signed in, you can use the `getSession` method of the `AmazonCognitoIdentityJS` class to get the user's session. The `getSession` method takes the following parameters:

* `accessToken`: The access token for the user.

The `getSession` method will return a `UserSession` object if the user is signed in. The `UserSession` object contains the same properties as the `UserSession` object that is returned by the `signIn` method.

The following code shows how to get a user's session:

```javascript
const AmazonCognitoIdentityJS = require('amazon-cognito-identity-js');

const clientId = '<your-client-id>';
const clientSecret = '<your-client-secret>';

const cognitoIdentity = new AmazonCognitoIdentityJS({
clientId,
clientSecret,
});

const accessToken = 'access-token';

const userSession = cognitoIdentity.getSession(accessToken);

console.log(userSession);
```

You can also use the `refreshSession` method of the `AmazonCognitoIdentityJS` class to refresh a user's session. The `refreshSession` method takes the following parameters:

* `refreshToken`: The refresh token for the user.

The `refreshSession` method will return a new `UserSession` object if the refresh token is valid.

The following code shows how to refresh a user's session:

```javascript
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top