-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c3a598
commit 5d872dc
Showing
24 changed files
with
350 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { UserData } from '@notespace/shared/src/users/types'; | ||
import { HttpCommunication } from '@services/communication/http/httpCommunication'; | ||
|
||
function authService(http: HttpCommunication) { | ||
async function registerUser(id: string, data: UserData) { | ||
await http.post('/users', { id, ...data }); | ||
} | ||
|
||
async function registerUserOAuth(id: string, data: UserData) { | ||
await http.post('/users?oauth=true', { id, ...data }); | ||
} | ||
|
||
async function getUser(id: string) { | ||
return await http.get(`/users/${id}`); | ||
} | ||
|
||
async function updateUser(id: string, newProps: Partial<UserData>) { | ||
await http.put(`/users/${id}`, { id, ...newProps }); | ||
} | ||
|
||
async function deleteUser(id: string) { | ||
await http.delete(`/users/${id}`); | ||
} | ||
|
||
return { | ||
registerUser, | ||
registerUserOAuth, | ||
getUser, | ||
updateUser, | ||
deleteUser, | ||
}; | ||
} | ||
|
||
export default authService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useMemo } from 'react'; | ||
import { useCommunication } from '@ui/contexts/communication/useCommunication'; | ||
import authService from '@services/auth/authService'; | ||
|
||
function useAuthService() { | ||
const { http } = useCommunication(); | ||
return useMemo(() => authService(http), [http]); | ||
} | ||
|
||
export default useAuthService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function validateUsername(username: string) { | ||
const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/; | ||
if (!usernameRegex.test(username)) { | ||
throw new Error('Invalid username'); | ||
} | ||
} | ||
|
||
export function validateEmail(email: string) { | ||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
if (!emailRegex.test(email)) { | ||
throw new Error('Invalid email'); | ||
} | ||
} | ||
|
||
export function validatePassword(password: string) { | ||
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/; | ||
if (!passwordRegex.test(password)) { | ||
throw new Error( | ||
'Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
begin; | ||
|
||
delete from "user"; | ||
delete from resource; | ||
delete from workspace; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
code/server/src/controllers/http/handlers/usersHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import PromiseRouter from 'express-promise-router'; | ||
import { Request, Response } from 'express'; | ||
import { UsersService } from '@services/UsersService'; | ||
import { httpResponse } from '@controllers/http/utils/httpResponse'; | ||
import { UserData } from '@notespace/shared/src/users/types'; | ||
|
||
function usersHandlers(service: UsersService) { | ||
const registerUser = async (req: Request, res: Response) => { | ||
const { id, ...data } = req.body; | ||
const oauth = req.query.oauth === 'true'; | ||
if (oauth) { | ||
try { | ||
const user = await service.getUser(id); | ||
if (user) { | ||
// user already registered | ||
httpResponse.noContent(res).send(); | ||
return; | ||
} | ||
} catch (e) { | ||
// user not found, continue | ||
} | ||
} | ||
await service.createUser(id, data); | ||
httpResponse.created(res).send(); | ||
}; | ||
|
||
const getUser = async (req: Request, res: Response) => { | ||
const { id } = req.params; | ||
const user = await service.getUser(id); | ||
httpResponse.ok(res).json(user); | ||
}; | ||
|
||
const updateUser = async (req: Request, res: Response) => { | ||
const { id } = req.params; | ||
const { ...data } = req.body as UserData; | ||
await service.updateUser(id, data); | ||
httpResponse.noContent(res).send(); | ||
}; | ||
|
||
const deleteUser = async (req: Request, res: Response) => { | ||
const { id } = req.params; | ||
await service.deleteUser(id); | ||
httpResponse.noContent(res).send(); | ||
}; | ||
|
||
const router = PromiseRouter({ mergeParams: true }); | ||
router.post('/', registerUser); | ||
router.get('/:id', getUser); | ||
router.put('/:id', updateUser); | ||
router.delete('/:id', deleteUser); | ||
|
||
return router; | ||
} | ||
|
||
export default usersHandlers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.