<p align="center">
  <a href="https://www.medusajs.com">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/59018053/229103275-b5e482bb-4601-46e6-8142-244f531cebdb.svg">
    <source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
    <img alt="Medusa logo" src="https://user-images.githubusercontent.com/59018053/229103726-e5b529a3-9b3f-4970-8a1f-c6af37f087bf.svg">
    </picture>
  </a>
</p>
<h1 align="center">
    Authentication OTP with AWS SNS
</h1>

<h4 align="center">
  <a href="https://docs.medusajs.com">Documentation</a> |
  <a href="https://www.medusajs.com">Website</a>
</h4>

## Compatibility

This starter is compatible with versions >= 2.6.1 of `@medusajs/medusa`. 


## Configuration

Add the plugin to your medusa-config.ts file:

```js
import { loadEnv, defineConfig } from '@medusajs/framework/utils'

loadEnv(process.env.NODE_ENV || 'development', process.cwd())

module.exports = defineConfig({
  modules: [
    {
      resolve: '@medusajs/medusa/auth',
      dependencies: [Modules.CACHE, ContainerRegistrationKeys.LOGGER],
      options: {
        providers: [
          {
            resolve: '@zimpligital/medusa-plugin-auth-otp/providers/auth-otp',
            id: 'auth-otp',
            dependencies: [Modules.CACHE, ContainerRegistrationKeys.LOGGER],
            options: {},
          },
        ],
      },
    },
  ],
  plugins: [
    {
      resolve: '@zimpligital/medusa-plugin-auth-otp',
      options: {
        awsSNSAccessKeyId: process.env.AWS_SNS_ACCESS_KEY_ID,
        awsSNSAccessKeySecret: process.env.AWS_SNS_SECRET_ACCESS_KEY,
        awsSNSRegion: process.env.AWS_SNS_REGION,
        jwtSecret: process.env.JWT_SECRET,
        otpConfigs: {
          subject: 'UTECH-OTP',
          message:
            'Your verification code is {otp} (ref. :{ref_code}) please verify within 90 secs',
          expiry: 90,
          webUrl: process.env.MEDUSA_FRONTEND_URL,
        },
      },
    },
  ],
})
```

## ENV variables
Add the environment variables to your .env
AWS_SNS_ACCESS_KEY_ID=
AWS_SNS_SECRET_ACCESS_KEY=
AWS_SNS_REGION=
MEDUSA_FRONTEND_URL=

## Provided APIs
- Request OTP <br />
&emsp;path: `{backend_url}/auth/customer/auth-otp/request` <br />
&emsp;method: `POST`  <br />
&emsp;body: 
```js
{
    "phone": "0999999999",
    "country_code": "66"
}
```

<h2>Responses</h2>
<h4 style="color: green">Success</h4>

```js
{
    "otp_request": {
        "id": "otp_r_01JP9E4JEQ8VWYRYG43R2KDKRR",
        "country_code": "66",
        "phone": "0999999999",
        "ref_code": "OEJC7J",
        "expired_at": "2025-03-14T04:22:28.521Z",
        "attempts": 0,
        "status": "pending",
        "created_at": "2025-03-14T04:20:58.455Z",
        "updated_at": "2025-03-14T04:20:58.528Z",
        "deleted_at": null
    },
    "token": "eyJhb..."
}
```
<h4 style="color: red">Failed 1: Too many requests</h4>

```js
{
    "code": "TOO_MANY_REQUESTS",
    "type": "invalid_data",
    "message": "Too many requests, please try again after 90 seconds"
}
```
<br /><br />
---
<br />

- Verify OTP <br />
&emsp;path: `{backend_url}/auth/customer/auth-otp/verify` <br />
&emsp;method: `POST`  <br />
&emsp;body: 
```js
{
    "phone": "0999999999",
    "otp": "703147",
    "ref_code": "FT0VNS"
}
```

<h2>Responses</h2>
<h4 style="color: green">Success</h4>

```js
{
    "success": true,
    "message": "OTP verified successfully",
    "token": "eyJhbG..."
}
```

<h4 style="color: red">Failed 1: Request not found</h4>

```js
{
    "code": "NOT_FOUND",
    "type": "not_found",
    "message": "Pending OTP request not found for phone: 0932856661 and ref_code: FT0VNS"
}
```

<h4 style="color: red">Failed 2: OTP expired</h4>

```js
{
    "code": "OTP_EXPIRED",
    "type": "invalid_data",
    "message": "OTP request expired"
}
```

<h4 style="color: red">Failed 3: OTP is invalid</h4>

```js
{
    "code": "INVALID_OTP",
    "type": "invalid_data",
    "message": "OTP is invalid"
}
```

<h4 style="color: red">Failed 4: OTP Attempts Exceeded</h4>

```js
{
    "code": "OTP_ATTEMPTS_EXCEEDED",
    "type": "invalid_data",
    "message": "OTP attemps exceeded"
}
```