Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' of https://github.com/CSCI-X050-A7/frontend into …
Browse files Browse the repository at this point in the history
…registration-functionality
  • Loading branch information
vrthelen committed Mar 17, 2024
2 parents 45aae40 + 580df53 commit 7964743
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 135 deletions.
177 changes: 163 additions & 14 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ export interface SchemaAuth {
export interface SchemaCreateUser {
/** @maxLength 150 */
email: string
/** @maxLength 100 */
first_name: string
is_active?: boolean
is_admin?: boolean
/** @maxLength 100 */
last_name: string
name: string
/**
* @minLength 10
* @minLength 8
* @maxLength 100
*/
password: string
/** @maxLength 20 */
phone: string
/**
* @minLength 5
* @minLength 3
* @maxLength 50
*/
username: string
Expand Down Expand Up @@ -80,19 +80,100 @@ export interface SchemaMovieListResponse {
offset?: number
}

export interface SchemaRegisterUser {
/** @maxLength 150 */
address: string
/** @maxLength 150 */
address2?: string
/** @maxLength 150 */
card_address?: string
/** @maxLength 150 */
card_address2?: string
/** @maxLength 100 */
card_city?: string
/** @maxLength 50 */
card_expiration?: string
/** @maxLength 50 */
card_number?: string
/** @maxLength 100 */
card_state?: string
/** @maxLength 50 */
card_type?: string
/** @maxLength 20 */
card_zip?: string
/** @maxLength 100 */
city: string
/** @maxLength 150 */
email: string
/** @maxLength 100 */
name: string
need_promotion?: boolean
/**
* @minLength 8
* @maxLength 100
*/
password: string
/** @maxLength 20 */
phone: string
/** @maxLength 100 */
state: string
/**
* @minLength 3
* @maxLength 50
*/
username: string
/** @maxLength 20 */
zip: string
}

export interface SchemaTokenResponse {
access_token?: string
msg?: string
redirect_url?: string
}

export interface SchemaUpdateUser {
/** @maxLength 150 */
address: string
/** @maxLength 150 */
address2?: string
/** @maxLength 150 */
card_address?: string
/** @maxLength 150 */
card_address2?: string
/** @maxLength 100 */
first_name: string
is_active?: boolean
is_admin?: boolean
card_city?: string
/** @maxLength 50 */
card_expiration?: string
/** @maxLength 50 */
card_number?: string
/** @maxLength 100 */
card_state?: string
/** @maxLength 50 */
card_type?: string
/** @maxLength 20 */
card_zip?: string
/** @maxLength 100 */
last_name: string
city: string
/** @maxLength 100 */
name: string
need_promotion?: boolean
/**
* @minLength 8
* @maxLength 100
*/
password: string
/** @maxLength 20 */
phone: string
/** @maxLength 100 */
state: string
/**
* @minLength 3
* @maxLength 50
*/
username: string
/** @maxLength 20 */
zip: string
}

export interface SchemaUpsertMovie {
Expand Down Expand Up @@ -120,15 +201,36 @@ export interface SchemaUpsertMovie {
}

export interface SchemaUser {
created_at?: string
email?: string
first_name?: string
id: string
is_active?: boolean
is_admin?: boolean
last_name?: string
updated_at?: string
name?: string
username?: string
}

export interface SchemaUserDetail {
address?: string
address2?: string
card_address?: string
card_address2?: string
card_city?: string
card_expiration?: string
card_number?: string
card_state?: string
card_type?: string
card_zip?: string
city?: string
email?: string
id?: string
is_active?: boolean
is_admin?: boolean
name?: string
need_promotion?: boolean
phone?: string
state?: string
username?: string
zip?: string
}

export interface SchemaUserListResponse {
Expand Down Expand Up @@ -512,6 +614,32 @@ export class Api<
})
}
auth = {
/**
* @description Activate a new user.
*
* @tags Auth
* @name V1AuthActivateCreate
* @summary activate
* @request POST:/api/v1/auth/activate
*/
v1AuthActivateCreate: (
query?: {
/** id */
id?: string
/** code */
code?: string
},
params: RequestParams = {}
) =>
this.request<SchemaUser, SchemaErrorResponse>({
path: `/api/v1/auth/activate`,
method: 'POST',
query: query,
type: ContentType.Json,
format: 'json',
...params
}),

/**
* @description Get current JWT.
*
Expand Down Expand Up @@ -574,6 +702,27 @@ export class Api<
type: ContentType.Json,
format: 'json',
...params
}),

/**
* @description Register for a new user.
*
* @tags Auth
* @name V1AuthRegisterCreate
* @summary register
* @request POST:/api/v1/auth/register
*/
v1AuthRegisterCreate: (
register: SchemaRegisterUser,
params: RequestParams = {}
) =>
this.request<SchemaUser, SchemaErrorResponse>({
path: `/api/v1/auth/register`,
method: 'POST',
body: register,
type: ContentType.Json,
format: 'json',
...params
})
}
movie = {
Expand Down Expand Up @@ -698,7 +847,7 @@ export class Api<
* @secure
*/
v1UsersMeList: (params: RequestParams = {}) =>
this.request<SchemaUser, SchemaErrorResponse>({
this.request<SchemaUserDetail, SchemaErrorResponse>({
path: `/api/v1/users/me`,
method: 'GET',
secure: true,
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// from https://github.com/sanjay-arya/react-auth-demo
// we use HttpOnly cookies from backend, not localStorage for security
import { useRequest } from 'ahooks'
import type { HttpResponse, SchemaUser } from 'client'
import type { HttpResponse, SchemaUserDetail } from 'client'
import type React from 'react'
import { createContext, useContext, useMemo, useState } from 'react'
import Backend from 'utils/service'

export interface AuthContextValue {
user: SchemaUser | undefined
user: SchemaUserDetail | undefined
loading: boolean
refresh: () => void
refreshAsync?: () => Promise<HttpResponse<SchemaUser>>
refreshAsync?: () => Promise<HttpResponse<SchemaUserDetail>>
}
const AuthContext = createContext<AuthContextValue>({
user: undefined,
Expand All @@ -19,7 +19,7 @@ const AuthContext = createContext<AuthContextValue>({
})

const AuthProvider: React.FC<React.PropsWithChildren> = ({ children }) => {
const [user, setUser] = useState<SchemaUser>()
const [user, setUser] = useState<SchemaUserDetail>()
const { loading, refresh, refreshAsync } = useRequest(
async () => Backend.user.v1UsersMeList(),
{
Expand Down
39 changes: 39 additions & 0 deletions src/pages/Activate/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useRequest } from 'ahooks'
import PageContainer from 'components/PageContainer'
import { useNavigate, useSearchParams } from 'react-router-dom'
import Backend from 'utils/service'

const Activate: React.FC = () => {
const navigate = useNavigate()
const [searchParams] = useSearchParams()
const id = searchParams.get('id') ?? ''
const code = searchParams.get('code') ?? ''
const { loading } = useRequest(
async () =>
Backend.auth.v1AuthActivateCreate({
id,
code
}),
{
onSuccess: () => {
navigate('/')
},
onError: () => {
navigate('/register')
}
}
)
return (
<div className='text-center'>
<h1>{loading ? 'Loading' : 'Redirecting'}</h1>
</div>
)
}

const Index: React.FC = () => (
<PageContainer>
<Activate />
</PageContainer>
)

export default Index
Loading

0 comments on commit 7964743

Please sign in to comment.