From 6c0c810d6a8113c8c64f73fc859fbbd9f3ee4e47 Mon Sep 17 00:00:00 2001 From: Arnab Chakraborty <11457760+Rocky43007@users.noreply.github.com> Date: Thu, 25 Jul 2024 00:17:33 +0300 Subject: [PATCH] Add Supertoken login page --- apps/desktop/package.json | 3 +- .../settings/client/account/Login.tsx | 133 +++++++--- .../settings/client/account/Register.tsx | 240 ++++++++++++------ .../client/account/handlers/cookieHandler.ts | 106 ++++++++ .../client/account/handlers/windowHandler.ts | 88 +++++++ interface/index.tsx | 22 ++ interface/package.json | 1 + pnpm-lock.yaml | Bin 1036427 -> 1037545 bytes 8 files changed, 474 insertions(+), 119 deletions(-) create mode 100644 interface/app/$libraryId/settings/client/account/handlers/cookieHandler.ts create mode 100644 interface/app/$libraryId/settings/client/account/handlers/windowHandler.ts diff --git a/apps/desktop/package.json b/apps/desktop/package.json index e354d3acd..d818718a6 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -29,7 +29,8 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "=6.20.1", - "sonner": "^1.0.3" + "sonner": "^1.0.3", + "supertokens-web-js": "^0.13.0" }, "devDependencies": { "@sd/config": "workspace:*", diff --git a/interface/app/$libraryId/settings/client/account/Login.tsx b/interface/app/$libraryId/settings/client/account/Login.tsx index 416f0a58b..c6580f307 100644 --- a/interface/app/$libraryId/settings/client/account/Login.tsx +++ b/interface/app/$libraryId/settings/client/account/Login.tsx @@ -1,33 +1,80 @@ -import { useZodForm } from '@sd/client'; -import { Button, Form, Input, z } from '@sd/ui'; - +import { Eye, EyeClosed } from '@phosphor-icons/react'; +import { useState } from 'react'; import { Controller } from 'react-hook-form'; +import { signIn, signUp } from 'supertokens-web-js/recipe/emailpassword'; +import { useZodForm } from '@sd/client'; +import { Button, Form, Input, toast, z } from '@sd/ui'; +async function signInClicked(email: string, password: string) { + try { + const response = await signIn({ + formFields: [ + { + id: 'email', + value: email + }, + { + id: 'password', + value: password + } + ] + }); + console.log('[signInClicked] response', response); + + if (response.status === 'FIELD_ERROR') { + response.formFields.forEach((formField) => { + if (formField.id === 'email') { + // Email validation failed (for example incorrect email syntax). + toast.error(formField.error); + } + }); + } else if (response.status === 'WRONG_CREDENTIALS_ERROR') { + toast.error('Email & password combination is incorrect.'); + } else if (response.status === 'SIGN_IN_NOT_ALLOWED') { + // the reason string is a user friendly message + // about what went wrong. It can also contain a support code which users + // can tell you so you know why their sign in was not allowed. + toast.error(response.reason); + } else { + // sign in successful. The session tokens are automatically handled by + // the frontend SDK. + console.log('Sign in successful'); + } + } catch (err: any) { + if (err.isSuperTokensGeneralError === true) { + // this may be a custom error message sent from the API by you. + toast.error(err.message); + } else { + toast.error('Oops! Something went wrong.'); + } + } +} const LoginSchema = z.object({ email: z.string().email(), - password: z.string().min(6), -}) + password: z.string().min(6) +}); const Login = () => { - const form = useZodForm( - { - schema: LoginSchema, - defaultValues: { - email: '', - password: '', - } - }) - return ( -
+ ); +}; -export default Login; \ No newline at end of file +export default Login; diff --git a/interface/app/$libraryId/settings/client/account/Register.tsx b/interface/app/$libraryId/settings/client/account/Register.tsx index eba8497f5..0551446d0 100644 --- a/interface/app/$libraryId/settings/client/account/Register.tsx +++ b/interface/app/$libraryId/settings/client/account/Register.tsx @@ -1,105 +1,181 @@ import { zodResolver } from '@hookform/resolvers/zod'; -import { Button, Form, Input, z } from '@sd/ui'; - +import { Eye, EyeClosed } from '@phosphor-icons/react'; +import { useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; +import { signUp } from 'supertokens-web-js/recipe/emailpassword'; +import { Button, Form, Input, toast, z } from '@sd/ui'; +const RegisterSchema = z + .object({ + email: z.string().email(), + password: z.string().min(6), + confirmPassword: z.string().min(6) + }) + .refine((data) => data.password === data.confirmPassword, { + message: 'Passwords do not match', + path: ['confirmPassword'] + }); +type RegisterType = z.infer