Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /register Query #55

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions src/app/register/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,27 @@ import { useForm, SubmitHandler } from 'react-hook-form'
import { useState } from 'react'
import { AiOutlineEye } from 'react-icons/ai'
import { AiOutlineEyeInvisible } from 'react-icons/ai'

interface signupInputs {
firstname: string
lastname: string
email: string
gender: string
birthday: any
country: string
city: string
password: string
confirmPassword: string
tnc: boolean | string
}
import { RegisterForm } from '@/models/auth'
import { useHandleRegister } from '@/services/auth'

const SignUp = () => {
const [showPassword, setShowPassword] = useState(false)
const [showConfirmPassword, setShowConfirmPassword] = useState(false)
const { mutate: mutateRegister } = useHandleRegister()
const {
register: registerSignup,
handleSubmit: handleSubmitSignup,
formState: { errors: signupErrors },
watch,
} = useForm<signupInputs>()
} = useForm<RegisterForm>()

// Get the current values of password and confirmPassword
const password = watch('password')

const onSignupSubmit: SubmitHandler<signupInputs> = (data) => {
const onSignupSubmit: SubmitHandler<RegisterForm> = (data) => {
console.log(data)
console.log('signup errors', signupErrors)
mutateRegister(data)
}

return (
Expand Down Expand Up @@ -101,7 +92,7 @@ const SignUp = () => {
<input
type="date"
{...registerSignup('birthday', { required: true })}
placeholder="Email Address"
placeholder="Birthday"
className=" border px-3 py-2 text-md rounded-lg border-gray-light font-normal text-gray"
/>
{signupErrors.birthday && <span className="text-red-500 font-normal">Please enter your birth date!</span>}
Expand All @@ -122,7 +113,7 @@ const SignUp = () => {
placeholder="City"
className=" border pl-3 py-2 text-md rounded-lg border-gray-light font-normal"
/>
{signupErrors.firstname && <span className="text-red-500 font-normal">Please enter your City!</span>}
{signupErrors.city && <span className="text-red-500 font-normal">Please enter your City!</span>}
<label htmlFor="password" className="py-1 mt-5">
Password
</label>
Expand Down Expand Up @@ -218,7 +209,7 @@ const SignUp = () => {
{/* checkbox for remember me */}
<div className="flex items-center pl-2">
<div>
<input {...registerSignup('tnc', { required: true })} type="checkbox" id="tnc" name="tnc" value="true" className="mr-2" />
<input {...registerSignup('tnc', { required: true })} type="checkbox" id="tnc" name="tnc" value={'true'} className="mr-2" />
</div>
<label htmlFor="tnc agree" className="text-md font-normal">
I have read and agree with the terms of service and privacy policy.
Expand Down
12 changes: 12 additions & 0 deletions src/models/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ export interface LoginForm {
email: string
password: string
}
export interface RegisterForm {
firstname: string
lastname: string
email: string
gender: string
birthday: any
country: string
city: string
password: string
confirmPassword: string
tnc: boolean | string
}

export interface UserResponseType {
user: User
Expand Down
27 changes: 26 additions & 1 deletion src/services/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoginForm, UserResponseType } from '@/models/auth'
import { LoginForm, RegisterForm, UserResponseType } from '@/models/auth'
import { useMutation } from '@tanstack/react-query'
import { client } from './api-Client'
import { useUniStore } from '@/store/store'
Expand All @@ -9,6 +9,11 @@ const login = async (data: LoginForm): Promise<UserResponseType> => {
return result
}

const register = async (data: RegisterForm): Promise<UserResponseType> => {
const result = await client<UserResponseType, RegisterForm>('auth/register', { data })
return result
}

export const useHandleLogin = () => {
const setUserData = useUniStore((state) => state.setUserData)
// const setToken = useUniStore((state) => state.setToken)
Expand All @@ -28,3 +33,23 @@ export const useHandleLogin = () => {
},
})
}

export const useHandleRegister = () => {
const setUserData = useUniStore((state) => state.setUserData)
// const setToken = useUniStore((state) => state.setToken)
const [_, setCookieValue] = useCookie('uni_user_token')
const [__, setRefreshCookieValue] = useCookie('uni_user_refresh_token')

return useMutation({
mutationFn: (data: RegisterForm) => register(data),
onSuccess: (response) => {
console.log(response, 'response')
console.log(_, __)

setUserData(response.user)
// setToken(response.tokens)
setCookieValue(response.tokens.access.token, response.tokens.access.expires)
setRefreshCookieValue(response.tokens.refresh.token, response.tokens.refresh.expires)
},
})
}
Loading