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' into remember-username
Browse files Browse the repository at this point in the history
  • Loading branch information
clovertera authored Mar 22, 2024
2 parents 5a44bcd + cc6b845 commit df3490e
Show file tree
Hide file tree
Showing 8 changed files with 603 additions and 255 deletions.
112 changes: 107 additions & 5 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ export interface SchemaUpdateUser {
/** @maxLength 100 */
name: string
need_promotion?: boolean
/**
* @minLength 8
* @maxLength 100
*/
password: string
/** @maxLength 20 */
phone: string
/** @maxLength 100 */
Expand Down Expand Up @@ -211,6 +206,12 @@ export interface SchemaUser {
username?: string
}

export interface SchemaUserChangePassword {
currentPassword?: string
newPassword?: string
username?: string
}

export interface SchemaUserDetail {
address?: string
address2?: string
Expand Down Expand Up @@ -242,6 +243,11 @@ export interface SchemaUserListResponse {
offset?: number
}

export interface SchemaUserResetPassword {
newPassword?: string
username?: string
}

export type QueryParamsType = Record<string | number, any>
export type ResponseFormat = keyof Omit<Body, 'body' | 'bodyUsed'>

Expand Down Expand Up @@ -642,6 +648,56 @@ export class Api<
...params
}),

/**
* @description Change the user's password.
*
* @tags Auth
* @name V1AuthChangepasswordCreate
* @summary change password
* @request POST:/api/v1/auth/changepassword
*/
v1AuthChangepasswordCreate: (
changePassword: SchemaUserChangePassword,
query?: {
/** Redirect url after login */
redirect_url?: string
},
params: RequestParams = {}
) =>
this.request<SchemaUserChangePassword, SchemaErrorResponse>({
path: `/api/v1/auth/changepassword`,
method: 'POST',
query: query,
body: changePassword,
type: ContentType.Json,
format: 'json',
...params
}),

/**
* @description Initiate the password reset process.
*
* @tags Auth
* @name V1AuthForgotpasswordCreate
* @summary forgot password
* @request POST:/api/v1/auth/forgotpassword
*/
v1AuthForgotpasswordCreate: (
query?: {
/** Email */
email?: string
},
params: RequestParams = {}
) =>
this.request<object, SchemaErrorResponse>({
path: `/api/v1/auth/forgotpassword`,
method: 'POST',
query: query,
type: ContentType.Json,
format: 'json',
...params
}),

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

/**
* @description Reset the user's password.
*
* @tags Auth
* @name V1AuthResetpasswordCreate
* @summary reset password
* @request POST:/api/v1/auth/resetpassword
*/
v1AuthResetpasswordCreate: (
resetPassword: SchemaUserResetPassword,
query?: {
/** Redirect url after login */
redirect_url?: string
},
params: RequestParams = {}
) =>
this.request<SchemaUserResetPassword, SchemaErrorResponse>({
path: `/api/v1/auth/resetpassword`,
method: 'POST',
query: query,
body: resetPassword,
type: ContentType.Json,
format: 'json',
...params
})
}
movie = {
Expand Down Expand Up @@ -856,6 +938,26 @@ export class Api<
type: ContentType.Json,
format: 'json',
...params
}),

/**
* @description update user info.
*
* @tags User
* @name V1UsersMeUpdate
* @summary update user info.
* @request PUT:/api/v1/users/me
* @secure
*/
v1UsersMeUpdate: (user: SchemaUpdateUser, params: RequestParams = {}) =>
this.request<SchemaUserDetail, SchemaErrorResponse>({
path: `/api/v1/users/me`,
method: 'PUT',
body: user,
secure: true,
type: ContentType.Json,
format: 'json',
...params
})
}
misc = {
Expand Down
122 changes: 122 additions & 0 deletions src/pages/ChangePassword/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { useRequest } from 'ahooks'
import PageContainer from 'components/PageContainer'
import { useAuth } from 'hooks/useAuth'
import { useState } from 'react'
import { Col, Row } from 'react-bootstrap'
import Button from 'react-bootstrap/Button'
import Form from 'react-bootstrap/Form'
import { Navigate, useNavigate, useSearchParams } from 'react-router-dom'
import { DOMAIN_HOST } from 'utils/constants'
import Backend from 'utils/service'

const ChangeForm: React.FC = () => {
const { user } = useAuth()
const [searchParams] = useSearchParams()
const [username, setUsername] = useState('')
const [currentPassword, setCurrentPassword] = useState('')
const [newPassword, setNewPassword] = useState('')
const navigate = useNavigate()
const { run: changePasssword } = useRequest(
async () => {
const from = searchParams.get('from') ?? '/'
return Backend.auth.v1AuthChangepasswordCreate(
{
username,
currentPassword,
newPassword
},
{
redirect_url: `${DOMAIN_HOST}${from}`
}
)
},
{
manual: true,
onSuccess: () => {
navigate('/logout') // Navigate to the login page
}
}
)

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
changePasssword()
}

return user ? (
<>
<div className='text-center'>
<h1>Reset Password</h1>
</div>
<Col xs={12} md={8} lg={6} className='mx-auto mt-3'>
<Form onSubmit={handleSubmit} validated>
<Form.Group as={Row} className='mb-3' controlId='formBasicEmail'>
<Form.Label className='text-sm-end' column sm={2}>
Username
</Form.Label>
<Col sm={10}>
<Form.Control
required
type='text'
placeholder='Username'
defaultValue={username}
onChange={e => {
setUsername(e.target.value)
}}
/>
</Col>
</Form.Group>
<Form.Group as={Row} className='mb-3' controlId='formBasicPassword'>
<Form.Label className='text-sm-end' column sm={2}>
Current Password
</Form.Label>
<Col sm={10}>
<Form.Control
required
type='password'
placeholder='Current Password'
defaultValue={currentPassword}
onChange={e => {
setCurrentPassword(e.target.value)
}}
/>
</Col>
</Form.Group>
<Form.Group as={Row} className='mb-3' controlId='formBasicPassword'>
<Form.Label className='text-sm-end' column sm={2}>
New Password
</Form.Label>
<Col sm={10}>
<Form.Control
required
type='password'
placeholder='Password'
defaultValue={newPassword}
onChange={e => {
setNewPassword(e.target.value)
}}
/>
</Col>
</Form.Group>
<Form.Group as={Row} className='mb-3'>
<Col sm={{ span: 10, offset: 2 }}>
<Button variant='primary' type='submit'>
Submit
</Button>
</Col>
</Form.Group>
</Form>
</Col>
</>
) : (
<Navigate to='/' />
)
}

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

export default Index
Loading

0 comments on commit df3490e

Please sign in to comment.