@ttoss/react-auth
Version:
ttoss authentication module for React apps.
235 lines (231 loc) • 6.09 kB
JavaScript
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
value,
configurable: true
});
// src/Auth.tsx
import { Auth as AuthCore, useAuthScreen } from "@ttoss/react-auth-core";
import { useI18n } from "@ttoss/react-i18n";
import { useNotifications } from "@ttoss/react-notifications";
import { confirmResetPassword, confirmSignUp, resendSignUpCode, resetPassword, signIn, signUp } from "aws-amplify/auth";
import * as React from "react";
var Auth = /* @__PURE__ */__name(props => {
const {
intl
} = useI18n();
const {
screen,
setScreen
} = useAuthScreen();
const {
addNotification
} = useNotifications();
const onSignIn = React.useCallback(async ({
email,
password
}) => {
try {
const result = await signIn({
username: email,
password
});
if (result.nextStep.signInStep === "RESET_PASSWORD") {
addNotification({
type: "error",
message: `For your security, we have updated our system and you need to reset your password in 'forgot your password?' to proceed`
});
} else if (result.nextStep.signInStep === "CONFIRM_SIGN_UP") {
await resendSignUpCode({
username: email
});
setScreen({
value: "confirmSignUpWithCode",
context: {
email
}
});
} else if (result.nextStep.signInStep === "DONE") {
addNotification({
viewType: "toast",
type: "success",
message: intl.formatMessage({
id: "EO/33N",
defaultMessage: [{
"type": 0,
"value": "Signed in successfully"
}]
})
});
}
} catch (error) {
addNotification({
type: "error",
message: error.message
});
}
}, [addNotification, intl, setScreen]);
const onSignUp = React.useCallback(async ({
email,
password
}) => {
try {
await signUp({
username: email,
password,
options: {
userAttributes: {
email
}
}
});
setScreen({
value: "confirmSignUpWithCode",
context: {
email
}
});
} catch (error) {
addNotification({
type: "error",
message: error.message
});
}
}, [setScreen, addNotification]);
const onConfirmSignUpWithCode = React.useCallback(async ({
email,
code
}) => {
try {
await confirmSignUp({
confirmationCode: code,
username: email
});
setScreen({
value: "signIn"
});
} catch (error) {
addNotification({
type: "error",
message: error.message
});
}
}, [setScreen, addNotification]);
const onForgotPassword = React.useCallback(async ({
email
}) => {
try {
await resetPassword({
username: email
});
setScreen({
value: "confirmResetPassword",
context: {
email
}
});
} catch (error) {
addNotification({
type: "error",
message: error.message
});
}
}, [setScreen, addNotification]);
const onForgotPasswordResetPassword = React.useCallback(async ({
email,
code,
newPassword
}) => {
try {
await confirmResetPassword({
confirmationCode: code,
username: email,
newPassword
});
setScreen({
value: "signIn"
});
} catch (error) {
addNotification({
type: "error",
message: error.message
});
}
}, [setScreen, addNotification]);
return /* @__PURE__ */React.createElement(AuthCore, {
screen,
setScreen,
onSignIn,
onSignUp,
onConfirmSignUpWithCode,
onForgotPassword,
onForgotPasswordResetPassword,
signUpTerms: props.signUpTerms,
logo: props.logo,
layout: props.layout
});
}, "Auth");
// src/AuthProvider.tsx
import { AuthProvider as AuthProviderCore, useAuth } from "@ttoss/react-auth-core";
import { signOut } from "aws-amplify/auth";
import { Hub } from "aws-amplify/utils";
import * as React2 from "react";
// src/getAuthData.ts
import { fetchAuthSession, fetchUserAttributes, getCurrentUser } from "aws-amplify/auth";
var getAuthData = /* @__PURE__ */__name(async ({
includeTokens
} = {}) => {
const currentUser = await getCurrentUser();
const [session, user] = await Promise.all([includeTokens ? fetchAuthSession() : Promise.resolve(null), fetchUserAttributes()]);
const idToken = session?.tokens?.idToken?.toString() ?? "";
const accessToken = session?.tokens?.accessToken?.toString() ?? "";
const refreshToken = "";
return {
user: {
id: currentUser.userId,
email: user.email ?? "",
emailVerified: user.email_verified === "true"
},
tokens: {
idToken,
accessToken,
refreshToken
},
isAuthenticated: true
};
}, "getAuthData");
var checkAuth = /* @__PURE__ */__name(async () => {
try {
const currentUser = await getCurrentUser();
return !!currentUser;
} catch {
return false;
}
}, "checkAuth");
// src/AuthProvider.tsx
var AuthProvider = /* @__PURE__ */__name(props => {
const [authListenerCount, setAuthListenerCount] = React2.useState(0);
React2.useEffect(() => {
const listener = /* @__PURE__ */__name(() => {
setAuthListenerCount(count => {
return count + 1;
});
}, "listener");
const stopHubListener = Hub.listen("auth", listener);
return () => {
stopHubListener();
};
}, []);
const getAuthDataCallback = React2.useCallback(async () => {
try {
return getAuthData();
} catch {
return null;
}
}, [authListenerCount]);
return /* @__PURE__ */React2.createElement(AuthProviderCore, {
getAuthData: getAuthDataCallback,
signOut
}, props.children);
}, "AuthProvider");
export { Auth, AuthProvider, checkAuth, getAuthData, useAuth };