Skip to content

Commit

Permalink
feature: OTP email configuration (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
skambalin authored Jul 31, 2024
1 parent a7401c5 commit 8db10ea
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 14 deletions.
1 change: 1 addition & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ REACT_APP_RAMP_API_KEY=e5vj99sfvuxcyt99y6u9bahbcxex4feo4y6eaqjx
# Feature flags
REACT_APP_TRANSFER_ASSETS_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ENABLE_OTP_LINK=true
3 changes: 2 additions & 1 deletion .env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ REACT_APP_SENTRY_DNS=https://[email protected]

# Feature flags
REACT_APP_TRANSFER_ASSETS_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ENABLE_OTP_LINK=true
5 changes: 5 additions & 0 deletions .env.prod-legacy
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ REACT_APP_GMT_ID=GTM-WQ33L7GG

# Reporting
REACT_APP_SENTRY_DNS=https://[email protected]/4505266227052544

# Feature flags
REACT_APP_TRANSFER_ASSETS_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ENABLE_OTP_LINK=true
1 change: 1 addition & 0 deletions .env.simulation-cyan
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ REACT_APP_SENTRY_DNS=https://[email protected]
# Feature flags
REACT_APP_TRANSFER_ASSETS_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ENABLE_OTP_LINK=true
1 change: 1 addition & 0 deletions .env.simulation-mainnet
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ REACT_APP_SENTRY_DNS=https://[email protected]
# Feature flags
REACT_APP_TRANSFER_ASSETS_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ENABLE_OTP_LINK=true
1 change: 1 addition & 0 deletions .env.simulation-testnet
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ REACT_APP_SENTRY_DNS=https://[email protected]
# Feature flags
REACT_APP_TRANSFER_ASSETS_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ENABLE_OTP_LINK=true
1 change: 1 addition & 0 deletions .env.stage
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ REACT_APP_SENTRY_DNS=https://[email protected]
# Feature flags
REACT_APP_TRANSFER_ASSETS_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ENABLE_OTP_LINK=false
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ REACT_APP_GMT_ID=GTM-WQ33L7GG
# Feature flags
REACT_APP_TRANSFER_ASSETS_FEATURE=true
REACT_APP_ASSETS_MANAGEMENT_FEATURE=true
REACT_APP_ENABLE_OTP_LINK=true
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### vNext

-
- Send OTP email with context specific values

### v1.40.0

Expand Down
1 change: 1 addition & 0 deletions packages/embed-wallet/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type ContextApp = {
appId?: string;
name?: string;
logoUrl?: string;
supportEmail?: string;
};

export type ContextWhiteLabel = Record<string, any>; // TODO: Use proper white label types
Expand Down
17 changes: 14 additions & 3 deletions src/api/auth-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { AxiosError, AxiosResponse } from 'axios';

import { reportError } from '~/reporting';
import { WALLET_API } from '~/constants';
import { FEATURE_FLAGS, WALLET_API } from '~/constants';
import { ApiResponse } from '~/api/interfaces';

export type TokenData = {
Expand All @@ -12,16 +12,27 @@ export type TokenByLinkData = TokenData & {
code: string;
};

export type SendOtpOptions = {
appTitle?: string;
supportEmail?: string;
authLink?: boolean;
popupId?: string;
};

const api = axios.create({
baseURL: WALLET_API,
});

export class AuthApiService {
public static async sendOtp(email: string): Promise<string | null> {
public static async sendOtp(email: string, options: SendOtpOptions = {}): Promise<string | null> {
let result: AxiosResponse<ApiResponse<{ authLinkCode: string }>> | null = null;

try {
result = await api.post('/auth/otp/send', { email });
result = await api.post('/auth/otp/send', {
...options,
email,
authLink: options.authLink ?? FEATURE_FLAGS.otpLink,
});
} catch (err: any) {
reportError(err);
}
Expand Down
18 changes: 14 additions & 4 deletions src/components/Login/OtpPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ export const OtpPage = ({ email, onRequestLogin, busy = false, code }: OtpProps)
const location = useLocation();
const navigate = useNavigate();
const [spamNotice, setSpamNotice] = useState(false);
const [otpAccepted, setOtpAccepted] = useState(false);
const [timeLeft, setTimeLeft] = useState<number>(TIME_LEFT);
const { isGame } = useTheme();
const store = useAppContextStore();
const appStore = useAppContextStore();

const verifyScreenSettings = store?.whiteLabel?.verifyScreenSettings;
const verifyScreenSettings = appStore?.whiteLabel?.verifyScreenSettings;

const {
control,
Expand Down Expand Up @@ -76,6 +77,8 @@ export const OtpPage = ({ email, onRequestLogin, busy = false, code }: OtpProps)
return setError('code', { message: 'The code is wrong, please try again' });
}

setOtpAccepted(true);

try {
await onRequestLogin(token);
} catch (error) {
Expand All @@ -89,7 +92,14 @@ export const OtpPage = ({ email, onRequestLogin, busy = false, code }: OtpProps)

const handleResend = async () => {
setTimeLeft(TIME_LEFT);
await AuthApiService.sendOtp(email!);

/**
* TODO: Use AuthorizePopupStore method to send OTP
*/
await AuthApiService.sendOtp(email!, {
appTitle: appStore.app?.name,
supportEmail: appStore.app?.supportEmail,
});
};

useEffect(() => {
Expand Down Expand Up @@ -203,7 +213,7 @@ export const OtpPage = ({ email, onRequestLogin, busy = false, code }: OtpProps)
</Typography>
)}

{spamNotice && (
{spamNotice && !otpAccepted && (
<Fade in>
<Alert variant="standard" severity="info">
If you didn’t get the verification email please check your Spam folder.
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const ANALYTICS = {
export const FEATURE_FLAGS = {
transferAssets: process.env.REACT_APP_TRANSFER_ASSETS_FEATURE === 'true',
assetsManagement: process.env.REACT_APP_ASSETS_MANAGEMENT_FEATURE === 'true',
otpLink: process.env.REACT_APP_ENABLE_OTP_LINK === 'true',
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/routes/Authorize/Authorize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Authorize = () => {
sessionNamespace,
loginHint,
email,
appId: wallet.appContextStore.app?.appId,
app: wallet.appContextStore.app,
}),
[wallet],
);
Expand Down
11 changes: 10 additions & 1 deletion src/stores/AppContextStore/AppContextStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@ export class AppContextStore {

const name = this.context.app.name || new URL(this.context.app.url).hostname;

return { ...this.context.app, name };
let supportEmail = this.context.app.supportEmail;

/**
* TODO: Remove this condition after Developer Console provide email via SDK
*/
if (!supportEmail && this.context.app.appId === 'developer-console') {
supportEmail = '[email protected]';
}

return { ...this.context.app, name, supportEmail };
}

async disconnect() {
Expand Down
10 changes: 7 additions & 3 deletions src/stores/AuthorizePopupStore/AuthorizePopupStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { reportError } from '~/reporting';
import { OpenLoginStore } from '../OpenLoginStore';
import { SessionStore } from '../SessionStore';
import { Web3AuthStore } from '../Web3AuthStore';
import { type App } from '../AppContextStore';
import { createSharedPopupState } from '../sharedState';
import { createRedirectUrl } from './createRedirectUrl';
import { Wallet } from '../types';
Expand All @@ -23,7 +24,7 @@ export type AuthorizePopupStoreOptions = {
redirectUrl?: string;
forceMfa?: boolean;
sessionNamespace?: string;
appId?: string;
app?: App;
loginHint?: string;
email?: string;
};
Expand Down Expand Up @@ -130,7 +131,7 @@ export class AuthorizePopupStore {

const { userInfo, permissions } = await this.web3AuthStore.login({
idToken,
appId: this.options.appId,
appId: this.options.app?.appId,
checkMfa: isMfa === undefined,
});

Expand Down Expand Up @@ -190,7 +191,10 @@ export class AuthorizePopupStore {
throw new Error('Email is required to send OTP');
}

const authLinkCode = await AuthApiService.sendOtp(toEmail);
const authLinkCode = await AuthApiService.sendOtp(toEmail, {
appTitle: this.options.app?.name,
supportEmail: this.options.app?.supportEmail,
});

if (authLinkCode) {
this.authLinkResource?.dispose();
Expand Down

0 comments on commit 8db10ea

Please sign in to comment.