Skip to content

Commit

Permalink
Add ability to send totp
Browse files Browse the repository at this point in the history
  • Loading branch information
lakhansamani committed Dec 2, 2023
1 parent 0d43c23 commit db4afa8
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 24 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@authorizerdev/authorizer-js": "^1.2.15"
"@authorizerdev/authorizer-js": "^1.2.17"
}
}
16 changes: 9 additions & 7 deletions src/components/AuthorizerBasicAuthLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { OtpDataType, TotpDataType } from '../types';
import { AuthorizerTOTPScanner } from './AuthorizerTOTPScanner';

const initOtpData: OtpDataType = {
isScreenVisible: false,
is_screen_visible: false,
email: '',
phone_number: '',
};

const initTotpData: TotpDataType = {
isScreenVisible: false,
is_screen_visible: false,
email: '',
phone_number: '',
authenticator_scanner_image: '',
Expand Down Expand Up @@ -85,7 +85,7 @@ export const AuthorizerBasicAuthLogin: FC<{
res.authenticator_recovery_codes
) {
setTotpData({
isScreenVisible: true,
is_screen_visible: true,
email: data.email || ``,
phone_number: data.phone_number || ``,
authenticator_scanner_image: res.authenticator_scanner_image,
Expand All @@ -101,9 +101,10 @@ export const AuthorizerBasicAuthLogin: FC<{
res?.should_show_totp_screen)
) {
setOtpData({
isScreenVisible: true,
is_screen_visible: true,
email: data.email || ``,
phone_number: data.phone_number || ``,
is_totp: res?.should_show_totp_screen || false,
});
return;
}
Expand Down Expand Up @@ -163,7 +164,7 @@ export const AuthorizerBasicAuthLogin: FC<{
}
}, [formData.password]);

if (totpData.isScreenVisible) {
if (totpData.is_screen_visible) {
return (
<AuthorizerTOTPScanner
{...{
Expand All @@ -181,14 +182,15 @@ export const AuthorizerBasicAuthLogin: FC<{
);
}

if (otpData.isScreenVisible) {
if (otpData.is_screen_visible) {
return (
<AuthorizerVerifyOtp
{...{
setView,
onLogin,
email: otpData.email || ``,
phone_number: otpData.phone_number || ``,
is_totp: otpData.is_totp || false,
}}
urlProps={urlProps}
/>
Expand All @@ -207,7 +209,7 @@ export const AuthorizerBasicAuthLogin: FC<{
className={styles['form-input-label']}
htmlFor="authorizer-login-email"
>
<span>* </span>Email
<span>* </span>Email / Phone Number
</label>
<input
name="email_or_phone_number"
Expand Down
13 changes: 8 additions & 5 deletions src/components/AuthorizerTOTPScanner.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC, useState } from 'react';
import { StyledButton } from '../styledComponents';
import { StyledButton, StyledFlex, StyledSeparator } from '../styledComponents';
import { ButtonAppearance, Views } from '../constants';
import { AuthorizerVerifyOtp } from './AuthorizerVerifyOtp';

Expand Down Expand Up @@ -46,17 +46,20 @@ export const AuthorizerTOTPScanner: FC<{
<p style={{ margin: '10px 0px', fontWeight: 'bold' }}>
Scan the QR code or enter the secret key into your authenticator app.
</p>
<img
src={`data:image/jpeg;base64,${authenticator_scanner_image}`}
alt="scanner"
/>
<StyledFlex justifyContent="center">
<img
src={`data:image/jpeg;base64,${authenticator_scanner_image}`}
alt="scanner"
/>
</StyledFlex>
<p style={{ margin: '10px 0px' }}>
If you are unable to scan the QR code, please enter the secret key
manually.
</p>
<p style={{ margin: '10px 0px', fontWeight: 'bold' }}>
{authenticator_secret}
</p>
<StyledSeparator />
<p style={{ margin: '10px 0px' }}>
If you lose access to your authenticator app, you can use the recovery
codes below to regain access to your account. Please save these codes
Expand Down
5 changes: 3 additions & 2 deletions src/components/AuthorizerVerifyOtp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const AuthorizerVerifyOtp: FC<{
email?: string;
phone_number?: string;
urlProps?: Record<string, any>;
}> = ({ setView, onLogin, email, phone_number, urlProps }) => {
is_totp?: boolean;
}> = ({ setView, onLogin, email, phone_number, urlProps, is_totp }) => {
const [error, setError] = useState(``);
const [successMessage, setSuccessMessage] = useState(``);
const [loading, setLoading] = useState(false);
Expand Down Expand Up @@ -52,7 +53,7 @@ export const AuthorizerVerifyOtp: FC<{
if (urlProps?.state) {
data.state = urlProps.state;
}

data.is_totp = !!is_totp;
const res = await authorizerRef.verifyOtp(data);
setLoading(false);

Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AuthorizerSocialLogin } from './components/AuthorizerSocialLogin';
import { AuthorizerResetPassword } from './components/AuthorizerResetPassword';
import { AuthorizerVerifyOtp } from './components/AuthorizerVerifyOtp';
import { AuthorizerRoot as Authorizer } from './components/AuthorizerRoot';
import { AuthorizerTOTPScanner } from './components/AuthorizerTOTPScanner';

export {
useAuthorizer,
Expand All @@ -22,4 +23,5 @@ export {
AuthorizerSocialLogin,
AuthorizerResetPassword,
AuthorizerVerifyOtp,
AuthorizerTOTPScanner,
};
2 changes: 1 addition & 1 deletion src/styledComponents/StyledSeparator.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { ReactNode } from 'react';
import styles from '../styles/default.css';

const StyledSeparator = ({ children }: { children: ReactNode }) => {
const StyledSeparator = ({ children }: { children?: ReactNode }) => {
return <div className={styles['styled-separator']}>{children}</div>;
};

Expand Down
5 changes: 3 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ export type AuthorizerContextPropsType = {
};

export type OtpDataType = {
isScreenVisible: boolean;
is_screen_visible: boolean;
email?: string;
phone_number?: string;
is_totp?: boolean;
};

export type TotpDataType = {
isScreenVisible: boolean;
is_screen_visible: boolean;
email?: string;
phone_number?: string;
authenticator_scanner_image: string;
Expand Down

0 comments on commit db4afa8

Please sign in to comment.