Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

feat: authorization #814

Merged
merged 17 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/graphql.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3667,7 +3667,7 @@
"description": null,
"args": [
{
"name": "id",
"name": "eventId",
"description": null,
"type": {
"kind": "NON_NULL",
Expand Down
26 changes: 13 additions & 13 deletions client/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export type QueryChapterUserArgs = {
};

export type QueryEventArgs = {
id: Scalars['Int'];
eventId: Scalars['Int'];
};

export type QueryEventsArgs = {
Expand Down Expand Up @@ -784,7 +784,7 @@ export type CreateEventMutation = {
};

export type UpdateEventMutationVariables = Exact<{
id: Scalars['Int'];
eventId: Scalars['Int'];
data: UpdateEventInputs;
}>;

Expand Down Expand Up @@ -905,7 +905,7 @@ export type EventsQuery = {
};

export type EventQueryVariables = Exact<{
id: Scalars['Int'];
eventId: Scalars['Int'];
}>;

export type EventQuery = {
Expand Down Expand Up @@ -969,7 +969,7 @@ export type EventQuery = {
};

export type EventVenuesQueryVariables = Exact<{
id: Scalars['Int'];
eventId: Scalars['Int'];
}>;

export type EventVenuesQuery = {
Expand Down Expand Up @@ -2089,8 +2089,8 @@ export type CreateEventMutationOptions = Apollo.BaseMutationOptions<
CreateEventMutationVariables
>;
export const UpdateEventDocument = gql`
mutation updateEvent($id: Int!, $data: UpdateEventInputs!) {
updateEvent(id: $id, data: $data) {
mutation updateEvent($eventId: Int!, $data: UpdateEventInputs!) {
updateEvent(id: $eventId, data: $data) {
id
name
canceled
Expand Down Expand Up @@ -2125,7 +2125,7 @@ export type UpdateEventMutationFn = Apollo.MutationFunction<
* @example
* const [updateEventMutation, { data, loading, error }] = useUpdateEventMutation({
* variables: {
* id: // value for 'id'
* eventId: // value for 'eventId'
* data: // value for 'data'
* },
* });
Expand Down Expand Up @@ -2574,8 +2574,8 @@ export type EventsQueryResult = Apollo.QueryResult<
EventsQueryVariables
>;
export const EventDocument = gql`
query event($id: Int!) {
event(id: $id) {
query event($eventId: Int!) {
event(eventId: $eventId) {
id
name
description
Expand Down Expand Up @@ -2651,7 +2651,7 @@ export const EventDocument = gql`
* @example
* const { data, loading, error } = useEventQuery({
* variables: {
* id: // value for 'id'
* eventId: // value for 'eventId'
* },
* });
*/
Expand Down Expand Up @@ -2680,8 +2680,8 @@ export type EventQueryResult = Apollo.QueryResult<
EventQueryVariables
>;
export const EventVenuesDocument = gql`
query eventVenues($id: Int!) {
event(id: $id) {
query eventVenues($eventId: Int!) {
event(eventId: $eventId) {
id
name
description
Expand Down Expand Up @@ -2719,7 +2719,7 @@ export const EventVenuesDocument = gql`
* @example
* const { data, loading, error } = useEventVenuesQuery({
* variables: {
* id: // value for 'id'
* eventId: // value for 'eventId'
* },
* });
*/
Expand Down
4 changes: 2 additions & 2 deletions client/src/modules/dashboard/Events/graphql/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const createEvent = gql`
`;

export const updateEvent = gql`
mutation updateEvent($id: Int!, $data: UpdateEventInputs!) {
updateEvent(id: $id, data: $data) {
mutation updateEvent($eventId: Int!, $data: UpdateEventInputs!) {
updateEvent(id: $eventId, data: $data) {
id
name
canceled
Expand Down
8 changes: 4 additions & 4 deletions client/src/modules/dashboard/Events/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const EVENTS = gql`
`;

export const EVENT = gql`
query event($id: Int!) {
event(id: $id) {
query event($eventId: Int!) {
event(eventId: $eventId) {
id
name
description
Expand Down Expand Up @@ -94,8 +94,8 @@ export const EVENT = gql`
`;

export const EVENT_WITH_VENU = gql`
query eventVenues($id: Int!) {
event(id: $id) {
query eventVenues($eventId: Int!) {
event(eventId: $eventId) {
id
name
description
Expand Down
8 changes: 4 additions & 4 deletions client/src/modules/dashboard/Events/pages/EditEventPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ import { HOME_PAGE_QUERY } from '../../../home/graphql/queries';
export const EditEventPage: NextPage = () => {
const router = useRouter();
const [loadingUpdate, setLoadingUpdate] = useState<boolean>(false);
const id = getId(router.query) || -1;
const eventId = getId(router.query) || -1;

const {
loading: eventLoading,
error,
data,
} = useEventQuery({
variables: { id },
variables: { eventId: eventId },
});

// TODO: update the cache directly:
// https://www.apollographql.com/docs/react/data/mutations/#updating-the-cache-directly
const [updateEvent] = useUpdateEventMutation({
refetchQueries: [
{ query: EVENTS },
{ query: EVENT, variables: { id } },
{ query: EVENT, variables: { eventId } },
{ query: HOME_PAGE_QUERY, variables: { offset: 0, limit: 2 } },
],
});
Expand Down Expand Up @@ -63,7 +63,7 @@ export const EditEventPage: NextPage = () => {
};

const event = await updateEvent({
variables: { id, data: { ...eventData } },
variables: { eventId, data: { ...eventData } },
});

if (event.data) {
Expand Down
6 changes: 3 additions & 3 deletions client/src/modules/dashboard/Events/pages/EventPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ import Actions from '../components/Actions';
import SponsorsCard from '../../../../components/SponsorsCard';
import { EVENT } from '../graphql/queries';

const args = (id: number) => ({
refetchQueries: [{ query: EVENT, variables: { id } }],
const args = (eventId: number) => ({
refetchQueries: [{ query: EVENT, variables: { eventId } }],
});

export const EventPage: NextPage = () => {
const router = useRouter();
const eventId = getId(router.query) || -1;
const { loading, error, data } = useEventQuery({
variables: { id: eventId },
variables: { eventId },
});

const [confirmRsvpFn] = useConfirmRsvpMutation(args(eventId));
Expand Down
17 changes: 9 additions & 8 deletions client/src/modules/events/pages/eventPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,21 @@ import {
import { useParam } from 'hooks/useParam';

export const EventPage: NextPage = () => {
const id = useParam('eventId');
const eventId = useParam('eventId');
const router = useRouter();
const { user } = useAuth();

const refetch = {
refetchQueries: [{ query: EVENT, variables: { id: id } }],
refetchQueries: [{ query: EVENT, variables: { eventId } }],
};

const [rsvpToEvent] = useRsvpToEventMutation(refetch);
const [initUserInterestForChapter] = useInitUserInterestForChapterMutation();
const [subscribeToEvent] = useSubscribeToEventMutation(refetch);
const [unsubscribeFromEvent] = useUnsubscribeFromEventMutation(refetch);
// TODO: check if we need to default to -1 here
const { loading, error, data } = useEventQuery({
variables: { id: id || -1 },
variables: { eventId: eventId || -1 },
});

const toast = useToast();
Expand All @@ -60,7 +61,7 @@ export const EventPage: NextPage = () => {
const ok = await confirm({ title: 'Do you want to subscribe?' });
if (ok) {
try {
await subscribeToEvent({ variables: { eventId: id } });
await subscribeToEvent({ variables: { eventId } });
toast({
title: 'You successfully subscribed to this event',
status: 'success',
Expand All @@ -79,7 +80,7 @@ export const EventPage: NextPage = () => {
});
if (ok) {
try {
await unsubscribeFromEvent({ variables: { eventId: id } });
await unsubscribeFromEvent({ variables: { eventId } });
toast({
title: 'You have unsubscribed from this event',
status: 'info',
Expand All @@ -101,7 +102,7 @@ export const EventPage: NextPage = () => {
if (ok) {
try {
await rsvpToEvent({
variables: { eventId: id },
variables: { eventId },
});

toast(
Expand All @@ -114,7 +115,7 @@ export const EventPage: NextPage = () => {
);
if (add) {
await initUserInterestForChapter({
variables: { event_id: id },
variables: { event_id: eventId },
});
}
} catch (err) {
Expand Down Expand Up @@ -148,7 +149,7 @@ export const EventPage: NextPage = () => {
if (canCheckRsvp) {
checkOnRsvp(true);
}
router.replace('/events/' + id, undefined, { shallow: true });
router.replace('/events/' + eventId, undefined, { shallow: true });
}
}, [allDataLoaded, canCheckRsvp]);

Expand Down
6 changes: 3 additions & 3 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ Cypress.Commands.add('getEventUsers', (eventId) => {
const eventQuery = {
operationName: 'eventUsers',
variables: {
id: eventId,
eventId,
},
query: `query eventUsers($id: Int!) {
event(id: $id) {
query: `query eventUsers($eventId: Int!) {
event(eventId: $eventId) {
event_users {
rsvp {
name
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"is-docker": "2.2.1",
"jest": "27.5.1",
"lint-staged": "12.3.8",
"lodash": "^4.17.21",
"prettier": "2.6.2",
"shx": "0.3.4",
"ts-jest": "27.1.4",
Expand Down
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"devDependencies": {
"@types/jsonwebtoken": "^8.5.8",
"@types/lodash": "^4.14.182",
"@types/nodemailer": "^6.4.4",
"chai": "4.3.6",
"concurrently": "7.1.0",
Expand Down
6 changes: 5 additions & 1 deletion server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import express, { Express, Response } from 'express';
// import isDocker from 'is-docker';
import { buildSchema } from 'type-graphql';

import { authorizationChecker } from './authorization';
import { GQLCtx, Request } from './common-types/gql';
import { resolvers } from './controllers';
import {
Expand All @@ -29,7 +30,10 @@ export const main = async (app: Express) => {
app.use(userMiddleware);
app.use(handleAuthenticationError);

const schema = await buildSchema({ resolvers });
const schema = await buildSchema({
resolvers,
authChecker: authorizationChecker,
});
const server = new ApolloServer({
schema,
context: ({ req, res }: { req: Request; res: Response }): GQLCtx => ({
Expand Down
Loading