Skip to content

Commit

Permalink
Merge pull request #538 from hackforla/host-and-coord-signup-endpoints
Browse files Browse the repository at this point in the history
Separate sign up route into host and coordinator sign up routes
  • Loading branch information
erikguntner authored Jul 19, 2023
2 parents 5076143 + d1b51af commit d20891f
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 51 deletions.
71 changes: 49 additions & 22 deletions api/openapi_server/_spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,37 @@ paths:
schema:
$ref: '#/components/schemas/ApiResponse'
x-openapi-router-controller: openapi_server.controllers.host_controller
/auth/signup:
/auth/signup/host:
post:
description: Signup a user
operationId: signup
description: Signup a Host
operationId: signUpHost
requestBody:
content:
application/json:
schema:
type: object
properties:
email:
type: string
password:
type: string
required:
- email
- password
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/ApiResponse'
description: successful operation
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.auth_controller
/auth/signup/coordinator:
post:
description: Sign up a Coordinator
operationId: signUpCoordinator
requestBody:
content:
application/json:
Expand Down Expand Up @@ -397,20 +424,26 @@ paths:
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.auth_controller
/auth/invite:
/auth/initialInvite:
post:
description: Invite user using AWS Congnito
operationId: invite
description: Removes auto generated password and replaces with user assigned password. Used for account setup.
operationId: initial_sign_in_reset_password
requestBody:
content:
application/json:
schema:
type: object
properties:
username:
email:
type: string
password:
type: string
session:
type: string
required:
- username
- email
- password
- session
responses:
'200':
content:
Expand All @@ -420,14 +453,11 @@ paths:
description: successful operation
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.auth_controller
security:
- jwt:
- secret
/auth/initialInvite:
x-openapi-router-controller: openapi_server.controllers.admin_controller
/auth/invite:
post:
description: Removes auto generated password and replaces with user assigned password. Used for account setup.
operationId: initial_sign_in_reset_password
description: Invite user using AWS Congnito
operationId: invite
requestBody:
content:
application/json:
Expand All @@ -436,14 +466,8 @@ paths:
properties:
email:
type: string
password:
type: string
session:
type: string
required:
- email
- password
- session
responses:
'200':
content:
Expand All @@ -453,7 +477,10 @@ paths:
description: successful operation
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.admin_controller
x-openapi-router-controller: openapi_server.controllers.auth_controller
security:
- jwt:
- secret
components:
securitySchemes:
jwt:
Expand Down
52 changes: 45 additions & 7 deletions api/openapi_server/controllers/auth_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def get_token_auth_header():
token = parts[1]
return token

def signup(): # noqa: E501
"""Signup a new user
def signUpHost(): # noqa: E501
"""Signup a new Host
"""
if connexion.request.is_json:
body = connexion.request.get_json()
Expand Down Expand Up @@ -123,6 +123,44 @@ def signup(): # noqa: E501

return response

def signUpCoordinator(): # noqa: E501
"""Signup a new Coordinator
"""
if connexion.request.is_json:
body = connexion.request.get_json()

secret_hash = get_secret_hash(body['email'])

# Signup user
with Session(db_engine) as session:
user = db.User(email=body['email'])
session.add(user)
try:
session.commit()
except IntegrityError:
session.rollback()
raise AuthError({
"message": "A user with this email already exists."
}, 422)

try:
response = userClient.sign_up(
ClientId=COGNITO_CLIENT_ID,
SecretHash=secret_hash,
Username=body['email'],
Password=body['password'],
)
except Exception as e:
code = e.response['Error']['Code']
message = e.response['Error']['Message']
status_code = e.response['ResponseMetadata']['HTTPStatusCode']

raise AuthError({
"code": code,
"message": message
}, status_code)

return response

def signin():
# Validate request data
Expand Down Expand Up @@ -437,20 +475,20 @@ def invite():
if connexion.request.is_json:
body = connexion.request.get_json()

if "username" not in body:
raise AuthError({"message": "username invalid"},400)
if "email" not in body:
raise AuthError({"message": "email invalid"},400)

try:

userName = body['username']
email = body['email']

response = userClient.admin_create_user(
UserPoolId=COGNITO_USER_POOL_ID,
Username=userName,
Username=email,
UserAttributes=[
{
'Name': "email",
'Value': userName
'Value': email
}
],
DesiredDeliveryMediums=["EMAIL"])
Expand Down
18 changes: 10 additions & 8 deletions api/openapi_server/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ paths:
/serviceProviders:
$ref: "./paths/serviceProviders.yaml"
/serviceProviders/{providerId}:
$ref: './paths/serviceProvider.yaml'
$ref: "./paths/serviceProvider.yaml"
/host:
$ref: './paths/host.yaml'
/auth/signup:
$ref: "./paths/auth/authSignup.yaml"
$ref: "./paths/host.yaml"
/auth/signup/host:
$ref: "./paths/auth/authSignupHost.yaml"
/auth/signup/coordinator:
$ref: "./paths/auth/authSignupCoordinator.yaml"
/auth/signin:
$ref: "./paths/auth/authSignin.yaml"
/auth/confirm:
Expand All @@ -36,13 +38,13 @@ paths:
/auth/user:
$ref: "./paths/auth/authUser.yaml"
/auth/private:
$ref: './paths/auth/authPrivate.yaml'
$ref: "./paths/auth/authPrivate.yaml"
/auth/google:
$ref: './paths/auth/authGoogle.yaml'
$ref: "./paths/auth/authGoogle.yaml"
/auth/initialInvite:
$ref: './paths/auth/authInitialInvite.yaml'
$ref: "./paths/auth/authInitialInvite.yaml"
/auth/invite:
$ref: './paths/auth/authInvite.yaml'
$ref: "./paths/auth/authInvite.yaml"
components:
securitySchemes:
jwt:
Expand Down
4 changes: 2 additions & 2 deletions api/openapi_server/openapi/paths/auth/authInvite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ post:
schema:
type: object
properties:
username:
email:
type: string
required:
- username
- email
responses:
'200':
content:
Expand Down
26 changes: 26 additions & 0 deletions api/openapi_server/openapi/paths/auth/authSignUpCoordinator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
post:
description: Sign up a Coordinator
operationId: signUpCoordinator
requestBody:
content:
application/json:
schema:
type: object
properties:
email:
type: string
password:
type: string
required:
- email
- password
responses:
"200":
content:
application/json:
schema:
$ref: "../../openapi.yaml#/components/schemas/ApiResponse"
description: successful operation
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.auth_controller
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
post:
description: Signup a user
operationId: signup
description: Signup a Host
operationId: signUpHost
requestBody:
content:
application/json:
Expand All @@ -23,4 +23,4 @@ post:
description: successful operation
tags:
- auth
x-openapi-router-controller: openapi_server.controllers.auth_controller
x-openapi-router-controller: openapi_server.controllers.auth_controller
10 changes: 5 additions & 5 deletions app/src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export interface UserResponse {
user: User;
}

export interface SignUpResponse {
export interface SignUpHostResponse {
user: User;
token: string;
}

export interface SignUpRequest {
export interface SignUpHostRequest {
email: string;
password: string;
}
Expand Down Expand Up @@ -59,9 +59,9 @@ export interface TokenResponse {

const authApi = api.injectEndpoints({
endpoints: build => ({
signUp: build.mutation<SignUpResponse, SignUpRequest>({
signUpHost: build.mutation<SignUpHostResponse, SignUpHostRequest>({
query: credentials => ({
url: '/auth/signup',
url: '/auth/signup/host',
method: 'POST',
headers: {
'Access-Control-Allow-Origin': 'http://localhost:4040',
Expand Down Expand Up @@ -161,7 +161,7 @@ const authApi = api.injectEndpoints({

export {authApi};
export const {
useSignUpMutation,
useSignUpHostMutation,
useSignInMutation,
useSignOutMutation,
useVerificationMutation,
Expand Down
8 changes: 4 additions & 4 deletions app/src/views/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {setCredentials} from '../app/authSlice';
import {useAppDispatch} from '../app/hooks/store';
import {SignUpForm} from '../components/authentication/SignUpForm';
import {
SignUpRequest,
useSignUpMutation,
SignUpHostRequest,
useSignUpHostMutation,
useGetTokenMutation,
} from '../services/auth';
// import {LocationState} from './SignIn';
Expand All @@ -21,7 +21,7 @@ export const SignUp = () => {
const navigate = useNavigate();
const location = useLocation();
const dispatch = useAppDispatch();
const [signUp] = useSignUpMutation();
const [signUp] = useSignUpHostMutation();
const [getToken] = useGetTokenMutation();
// const locationState = location.state as LocationState;

Expand Down Expand Up @@ -58,7 +58,7 @@ export const SignUp = () => {
setDialogOpen(false);
};

const handleSignUp = async ({email, password}: SignUpRequest) => {
const handleSignUp = async ({email, password}: SignUpHostRequest) => {
try {
await signUp({
email,
Expand Down

0 comments on commit d20891f

Please sign in to comment.