

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Amazon Cognito 進行身分驗證
<a name="authenticating-using-cognito"></a>

您可以使用 Amazon Cognito 身分驗證作為直接搭配前端 SDK 請求使用 AWS Identity and Access Management (IAM) 使用者的替代方案。

Amazon Cognito 為 Web 和行動應用程式提供身分驗證、授權和使用者管理。您可以將 Amazon Cognito 未驗證的身分集區與 Amazon Location 搭配使用，以讓應用程式擷取暫時、縮小範圍的 AWS 登入資料。

如需詳細資訊，請參閱《*Amazon Cognito 開發人員指南*》中的[使用者集區入門](https://docs.aws.amazon.com/cognito/latest/developerguide/getting-started-user-pools.html)。

您可能因為下列原因想要使用此形式的身分驗證：
+ **未驗證的使用者** – 如果您有匿名使用者的網站，您可以使用 Amazon Cognito 身分集區。

  如需詳細資訊，請參閱 上的 一節[使用 Amazon Cognito 進行身分驗證](#authenticating-using-cognito)。
+ **您自己的身分驗證** – 如果您想要使用自己的身分驗證程序，或結合多個身分驗證方法，您可以使用 Amazon Cognito 聯合身分。

  如需詳細資訊，請參閱《*Amazon Cognito 開發人員指南*》中的[聯合身分入門](https://docs.aws.amazon.com/cognito/latest/developerguide/getting-started-with-identity-pools.html)。

## 使用 Amazon Cognito 和 Amazon Location Service
<a name="cognito-and-location"></a>

您可以使用與未驗證身分角色相關聯的 AWS Identity and Access Management (IAM) 政策搭配下列動作：

------
#### [ Maps ]

映射動作清單
+ `geo-maps:GetStaticMap`
+ `geo-maps:GetTile`

**注意**  
上述動作的資源名稱為：  

```
arn:aws:geo-maps:region::provider/default
```

------
#### [ Places ]

位置動作清單：
+ `geo-places:Geocode`
+ `geo-places:ReverseGeocode`
+ `geo-places:SearchNearby`
+ `geo-places:SearchText`
+ `geo-places:Autocomplete`
+ `geo-places:Suggest`
+ `geo-places:GetPlace`

**注意**  
上述動作的資源名稱為：  

```
arn:aws:geo-places:region::provider/default
```

------
#### [ Routes ]

路由動作的清單：
+ `geo-routes:CalculateRoutes`
+ `geo-routes:CalculateRouteMatrix`
+ `geo-routes:CalculateIsolines`
+ `geo-routes:OptimizeWaypoints`
+ `geo-routes:SnapToRoads`

**注意**  
上述動作的資源名稱為：  

```
arn:aws:geo-routes:region::provider/default
```

------
#### [ Geofences and Trackers ]

Geofences 和 Trackers 動作的清單
+ `geo:GetGeofence`
+ `geo:ListGeofences`
+ `geo:PutGeofence`
+ `geo:BatchDeleteGeofence`
+ `geo:BatchPutGeofence`
+ `geo:BatchEvaluateGeofences`
+ `geo:GetDevicePosition*`
+ `geo:ListDevicePositions`
+ `geo:BatchDeleteDevicePositionHistory`
+ `geo:BatchGetDevicePosition`
+ `geo:BatchUpdateDevicePosition`

**注意**  
上述動作的資源名稱為：  

```
arn:aws:geo:region:accountID:tracker/ExampleTracker
```

------
#### [ Previous version ]

先前版本動作的清單：
+ `geo:GetMap*`
+ `geo:SearchPlaceIndexForText`
+ `geo:SearchPlaceIndexForPosition`
+ `geo:GetPlace`
+ `geo:CalculateRoute`
+ `geo:CalculateRouteMatrix`

**注意**  
上述動作的資源名稱為：  
**地圖**  

```
arn:aws:geo:region:accountID:map/ExampleMap
```
**位置**  

```
arn:aws:geo:region:accountID:place-index/ExamplePlaceIndex
```
**路由**  

```
arn:aws:geo:region:accountID:route-calculator/ExampleCalculator
```

------

## 建立 Amazon Cognito 身分集區
<a name="cognito-create-user-pool"></a>

您可以建立 Amazon Cognito 身分集區，以允許未經驗證的訪客透過 Amazon Cognito 主控台 AWS CLI、 或 Amazon Cognito APIs 存取您的應用程式。

**重要**  
您建立的集區必須與您正在使用的 Amazon Location Service 資源位於相同的 AWS 帳戶 和 AWS 區域。

------
#### [ Console ]

**使用 Amazon Cognito 主控台建立身分集區**

1. 前往 [Amazon Cognito 主控台](https://console.aws.amazon.com/cognito/home)。

1. 選擇 **Manage Identity Pools (管理身分集區)**。

1. 選擇**建立新的身分集區**，然後輸入身分集區的名稱。

1. 從**未驗證的身分**可折疊區段中，選擇**啟用存取未驗證的身分**。

1. 選擇 **Create Pool** (建立集區)。

1. 選擇您要搭配身分集區使用的 IAM 角色。

1. 展開**檢視詳細資訊**。

1. 在**未驗證的身分**下，輸入角色名稱。

1. 展開**檢視政策文件**區段，然後選擇**編輯**以新增您的政策。

1. 新增您的政策以授予 資源的存取權。
**注意**  
如需動作清單，請參閱上[使用 Amazon Cognito 和 Amazon Location Service](#cognito-and-location)一節。

   ```
   {
     "Version": "2012-10-17",		 	 	 
     "Statement": [
       {
     Sid": "RoutesReadOnly",
     Effect": "Allow",
     Action": [
           // add comma separated value from the previous section
         ],
     Resource": "value from previous section"
       }
     ]
   }
   ```

1. 選擇**允許**以建立您的身分集區。

------

## 在 Web 中使用 Amazon Cognito 身分集區
<a name="identity-pool-js"></a>

下列範例會交換您為登入資料建立的未驗證身分集區，然後用於呼叫 `CalculateIsolines`。為了簡化此工作，範例使用 Amazon Location [如何使用身分驗證協助程式](how-to-auth-helper.md) 程序。這是用來取代取得和重新整理登入資料。

此範例使用適用於 JavaScript v3 的 AWS SDK。

```
import { GeoRoutesClient, CalculateIsolinesCommand , } from "@aws-sdk/client-geo-routes"; // ES Modules import
import { withIdentityPoolId } from "@aws/amazon-location-utilities-auth-helper";

const identityPoolId = "<identity pool ID>"; // for example, us-east-1:1sample4-5678-90ef-aaaa-1234abcd56ef

const authHelper = await withIdentityPoolId(identityPoolId);

const client = new GeoRoutesClient({
    ...authHelper.getClientConfig(),
    region: "<region>", // The region containing the identity pool
});

const input = {
    DepartNow: true,
    TravelMode: "Car",
    Origin: [-123.12327, 49.27531],
    Thresholds: {
        Time: [5, 10, 30],
    },
};

const command = new CalculateIsolinesCommand(input);
const response = await client.send(command);

console.log(JSON.stringify(response, null, 2))
```