Skip to content

Commit

Permalink
no more custom header
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherJMiller committed Jan 12, 2024
1 parent 96d9c42 commit 6c8e711
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
16 changes: 9 additions & 7 deletions api/src/connections/connection.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import {
Request,
Headers,
Controller,
Post,
UseGuards,
Delete,
Body,
} from '@nestjs/common';
import { ConnectionsService } from './connections.service';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiHeader } from '@nestjs/swagger';
import { ApiBearerAuth, ApiBody } from '@nestjs/swagger';
import { authenticateAgainstMinecraft } from 'src/utils/minecraft';
import { User } from 'src/users/user.entity';
import { MinecraftToken } from './connection.entity';

@Controller()
export class ConnectionController {
Expand All @@ -19,16 +20,17 @@ export class ConnectionController {
@Post('connections/minecraft')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiHeader({
name: 'MS-Authorization',
@ApiBody({
required: true,
type: MinecraftToken,
description: 'payload containing the token',
})
async addMinecraft(
@Headers('MS-Authorization') msAccessToken: string,
@Body() tokenPayload: MinecraftToken,
@Request() req,
): Promise<void> {
console.log(msAccessToken, req.headers);
const user = User.fromJwt(req.user);
const id = await authenticateAgainstMinecraft(msAccessToken);
const id = await authenticateAgainstMinecraft(tokenPayload.token);
await this.connectionService.saveForUser(user, { minecraft_uuid: id });
}

Expand Down
4 changes: 4 additions & 0 deletions api/src/connections/connection.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';

export class MinecraftToken {
token: string;
}

@Entity()
export class Connection {
@PrimaryColumn()
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/pages/MinecraftConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export function MinecraftConnection() {
const run = async () => {
try {
console.log('Sending', msAccessToken);
const { error } = await addMinecraftToUser(token, msAccessToken);
const { error } = await addMinecraftToUser(token, {
token: msAccessToken,
});

if (error) {
setMessage(JSON.stringify(error));
Expand Down
9 changes: 3 additions & 6 deletions frontend/src/util/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Group = components['schemas']['Group'];
export type User = components['schemas']['User'];
export type NewGroup = components['schemas']['NewGroup'];
export type UpdateUser = components['schemas']['UpdateUser'];
export type MinecraftToken = components['schemas']['MinecraftToken'];

const { GET, POST, PATCH } = createClient<paths>({ baseUrl: API_URL });

Expand Down Expand Up @@ -39,16 +40,12 @@ export const updateUser = (token: string, username: string, body: UpdateUser) =>
body,
});

export const addMinecraftToUser = (token: string, msToken: string) =>
export const addMinecraftToUser = (token: string, body: MinecraftToken) =>
POST('/api/connections/minecraft', {
headers: {
Authorization: `Bearer ${token}`,
},
params: {
header: {
'MS-Authorization': msToken,
},
},
body,
});

export const group = (token: string, id: string) =>
Expand Down
10 changes: 7 additions & 3 deletions frontend/src/util/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export interface components {
description?: string;
pronouns?: string;
};
MinecraftToken: {
token: string;
};
NewGroup: {
name: string;
};
Expand Down Expand Up @@ -131,9 +134,10 @@ export interface operations {
};
};
ConnectionController_addMinecraft: {
parameters: {
header: {
"MS-Authorization": string;
/** @description payload containing the token */
requestBody: {
content: {
"application/json": components["schemas"]["MinecraftToken"];
};
};
responses: {
Expand Down

0 comments on commit 6c8e711

Please sign in to comment.