Skip to content

Commit

Permalink
Made changes so auth on openapi schema works. (#143)
Browse files Browse the repository at this point in the history
* Made changes so auth on openapi schema works.

Now requests that require BE to check token will be sent with
Bearer JWT from FE as a token.

* Made changes based on review on PR #143

* Okay actual changes reflecting review on PR #143.
  • Loading branch information
thai-truong authored and godwinpang committed Mar 13, 2021
1 parent beacdbe commit 407a797
Show file tree
Hide file tree
Showing 13 changed files with 561 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"storybook": "start-storybook -p 6006 -s public",
"build-storybook": "build-storybook -s public",
"precodegen": "rimraf src/api/*",
"codegen": "npx openapi-generator generate -i http://dev.api.hknucsd.com/api/docs/json -g typescript-fetch --additional-properties=typescriptThreePlus=true -o src/api/"
"codegen": "npx openapi-generator generate -i http://dev.api.hknucsd.com/api/docs/json -g typescript-fetch --additional-properties=typescriptThreePlus=true -o src/services/api"
},
"eslintConfig": {
"extends": "react-app"
Expand Down
10 changes: 9 additions & 1 deletion src/pages/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import {
InducteeRoutingPermission,
OfficerRoutingPermission,
} from '@HOCs/RoutingPermissions';
import ApiConfigStore from '@Services/ApiConfigStore';

const INITIAL_STATES = {
userClaims: null,
userToken: null,
isLoading: true,
};

Expand All @@ -42,25 +44,31 @@ class App extends React.Component {
firebase.auth().onAuthStateChanged(async user => {
if (user) {
const tokenResult = await user.getIdTokenResult();
const { claims } = tokenResult;
const { claims, token } = tokenResult;

this.setState({
userClaims: {
userId: claims.user_id,
userRoles: getRolesFromClaims(claims),
},
userToken: token,
isLoading: false,
});
} else {
this.setState({
userClaims: null,
userToken: null,
isLoading: false,
});
}
});
}

setClaims = claims => {
const { userToken } = this.state;

ApiConfigStore.setToken(userToken);

this.setState({
userClaims: {
userId: claims.user_id,
Expand Down
5 changes: 5 additions & 0 deletions src/pages/EventSignInPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import styles from './styles';
import HKN_TRIDENT_LOGO from '@Images/hkn-trident.png';
import { Loading } from '@SharedComponents';
import { getEventById } from '@Services/events';
import { signInToEvent } from '@Services/ApiEvents';

class EventSignInPage extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -39,8 +40,12 @@ class EventSignInPage extends React.Component {
}

handleSubmit = (values, setSubmitting) => {
const { eventId } = this.state;

console.log(values);

signInToEvent(eventId, values);

setSubmitting(false);
};

Expand Down
2 changes: 1 addition & 1 deletion src/services/ApiConfigStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ApiConfigStoreClass {

// this is gonna be called on login
setToken(token: string) {
this.configParams.apiKey = token;
this.configParams.accessToken = token;
this.config = new Configuration(this.configParams);
}
}
Expand Down
80 changes: 78 additions & 2 deletions src/services/ApiEvents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { EventApi, EventControllerGetEventRequest } from './api/apis/EventApi';
import { MultipleEventResponse, EventResponse } from './api/models';
import {
EventApi,
EventControllerGetEventRequest,
EventControllerCreateEventRequest,
EventControllerDeleteEventRequest,
EventControllerUpdateEventRequest,
EventControllerSignInToEventRequest,
EventControllerRsvpForEventRequest,
} from './api/apis/EventApi';
import {
MultipleEventResponse,
EventResponse,
EventRequest,
AppUserEventRequest,
AttendanceResponse,
RSVPResponse,
} from './api/models';
import ApiConfigStore from './ApiConfigStore';
import { Configuration } from './api/runtime';

Expand All @@ -17,3 +32,64 @@ export async function getEventById(eventID: number): Promise<EventResponse> {
};
return eventApi.eventControllerGetEvent(request);
}

export async function createEvent(
eventRequest: EventRequest
): Promise<EventResponse> {
const apiConfig: Configuration = ApiConfigStore.getApiConfig();
const eventApi: EventApi = new EventApi(apiConfig);
const request: EventControllerCreateEventRequest = {
eventRequest,
};

return eventApi.eventControllerCreateEvent(request);
}

export async function updateEvent(eventID: number, eventRequest: EventRequest) {
const apiConfig: Configuration = ApiConfigStore.getApiConfig();
const eventApi: EventApi = new EventApi(apiConfig);
const request: EventControllerUpdateEventRequest = {
eventID,
eventRequest,
};

return eventApi.eventControllerUpdateEvent(request);
}

export async function deleteEvent(eventID: number): Promise<EventResponse> {
const apiConfig: Configuration = ApiConfigStore.getApiConfig();
const eventApi: EventApi = new EventApi(apiConfig);
const request: EventControllerDeleteEventRequest = {
eventID,
};

return eventApi.eventControllerDeleteEvent(request);
}

export async function signInToEvent(
eventID: number,
appUserEventRequest: AppUserEventRequest
): Promise<AttendanceResponse> {
const apiConfig: Configuration = ApiConfigStore.getApiConfig();
const eventApi: EventApi = new EventApi(apiConfig);
const request: EventControllerSignInToEventRequest = {
eventID,
appUserEventRequest,
};

return eventApi.eventControllerSignInToEvent(request);
}

export async function rsvpToEvent(
eventID: number,
appUserEventRequest: AppUserEventRequest
): Promise<RSVPResponse> {
const apiConfig: Configuration = ApiConfigStore.getApiConfig();
const eventApi: EventApi = new EventApi(apiConfig);
const request: EventControllerRsvpForEventRequest = {
eventID,
appUserEventRequest,
};

return eventApi.eventControllerRsvpForEvent(request);
}
36 changes: 20 additions & 16 deletions src/services/api/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
apis/EventApi.ts
apis/index.ts
index.ts
models/AppUserEventRequest.ts
models/AppUserEventResponse.ts
models/AppUserPKPayload.ts
models/AttendanceResponse.ts
models/BaseEventPayload.ts
models/EventAttendanceResponse.ts
models/EventRSVPResponse.ts
models/EventRequest.ts
models/EventResponse.ts
models/MultipleEventResponse.ts
models/RSVPResponse.ts
models/index.ts
runtime.ts
apis\EventApi.ts
apis\UserApi.ts
apis\index.ts
index.ts
models\AppUserEventRequest.ts
models\AppUserEventResponse.ts
models\AppUserInductionClass.ts
models\AppUserPKPayload.ts
models\AppUserProfileResponse.ts
models\AppUserRolesResponse.ts
models\AttendanceResponse.ts
models\BaseEventPayload.ts
models\EventAttendanceResponse.ts
models\EventRSVPResponse.ts
models\EventRequest.ts
models\EventResponse.ts
models\MultipleEventResponse.ts
models\RSVPResponse.ts
models\index.ts
runtime.ts
45 changes: 45 additions & 0 deletions src/services/api/apis/EventApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ export class EventApi extends runtime.BaseAPI {

headerParameters['Content-Type'] = 'application/json';

if (this.configuration && this.configuration.accessToken) {
const token = this.configuration.accessToken;
const tokenString =
typeof token === 'function' ? token('TokenAuth', []) : token;

if (tokenString) {
headerParameters['Authorization'] = `Bearer ${tokenString}`;
}
}
const response = await this.request({
path: `/api/events/`,
method: 'POST',
Expand Down Expand Up @@ -122,6 +131,15 @@ export class EventApi extends runtime.BaseAPI {

const headerParameters: runtime.HTTPHeaders = {};

if (this.configuration && this.configuration.accessToken) {
const token = this.configuration.accessToken;
const tokenString =
typeof token === 'function' ? token('TokenAuth', []) : token;

if (tokenString) {
headerParameters['Authorization'] = `Bearer ${tokenString}`;
}
}
const response = await this.request({
path: `/api/events/{eventID}`.replace(
`{${'eventID'}}`,
Expand Down Expand Up @@ -246,6 +264,15 @@ export class EventApi extends runtime.BaseAPI {

headerParameters['Content-Type'] = 'application/json';

if (this.configuration && this.configuration.accessToken) {
const token = this.configuration.accessToken;
const tokenString =
typeof token === 'function' ? token('TokenAuth', []) : token;

if (tokenString) {
headerParameters['Authorization'] = `Bearer ${tokenString}`;
}
}
const response = await this.request({
path: `/api/events/{eventID}/rsvp`.replace(
`{${'eventID'}}`,
Expand Down Expand Up @@ -296,6 +323,15 @@ export class EventApi extends runtime.BaseAPI {

headerParameters['Content-Type'] = 'application/json';

if (this.configuration && this.configuration.accessToken) {
const token = this.configuration.accessToken;
const tokenString =
typeof token === 'function' ? token('TokenAuth', []) : token;

if (tokenString) {
headerParameters['Authorization'] = `Bearer ${tokenString}`;
}
}
const response = await this.request({
path: `/api/events/{eventID}/signin`.replace(
`{${'eventID'}}`,
Expand Down Expand Up @@ -346,6 +382,15 @@ export class EventApi extends runtime.BaseAPI {

headerParameters['Content-Type'] = 'application/json';

if (this.configuration && this.configuration.accessToken) {
const token = this.configuration.accessToken;
const tokenString =
typeof token === 'function' ? token('TokenAuth', []) : token;

if (tokenString) {
headerParameters['Authorization'] = `Bearer ${tokenString}`;
}
}
const response = await this.request({
path: `/api/events/{eventID}`.replace(
`{${'eventID'}}`,
Expand Down
Loading

0 comments on commit 407a797

Please sign in to comment.