Skip to content

Commit

Permalink
update oauth and sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
dzencot committed Apr 11, 2023
1 parent 9b713b0 commit 351bf4b
Show file tree
Hide file tree
Showing 15 changed files with 75,160 additions and 14,647 deletions.
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@nestjs/testing": "^9.3.9",
"@nestjs/typeorm": "^9.0.1",
"@nestjs/websockets": "^9.3.9",
"@ntegral/nestjs-sentry": "^4.0.0",
"@popperjs/core": "^2.11.6",
"@reduxjs/toolkit": "^1.9.3",
"@sentry/node": "^7.47.0",
Expand Down
2 changes: 1 addition & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { MailerModule } from '@nestjs-modules/mailer';
import * as Sentry from '@sentry/node';
import { SentryModule } from '@ntegral/nestjs-sentry';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { SnippetsController } from './snippets/snippets.controller';
Expand All @@ -27,7 +28,6 @@ import { EventsModule } from './events/events.module';
import getMailerConfig from './config/mailer.config';
import getSentryConfig from './config/sentry.config';

import { SentryModule } from './sentry/sentry.module';
import '@sentry/tracing';

@Module({
Expand Down
23 changes: 19 additions & 4 deletions backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/* eslint-disable class-methods-use-this */
import { Controller, Post, UseGuards, Res, Req } from '@nestjs/common';
import {
Controller,
Get,
Post,
UseGuards,
Res,
Req,
Query,
} from '@nestjs/common';
import { Response } from 'express';
import { LocalAuthGuard } from './local-auth.guard';
import { AuthService } from './auth.service';
Expand All @@ -23,8 +31,15 @@ export class AuthController {
response.send();
}

@Post('oauth')
async oAuth(@Req() req, @Res() response: Response) {
return this.authService.oAuthGithub(req.body.code, response);
@Get('oauth')
async oAuth(@Query('code') code, @Req() req, @Res() response: Response) {
if (!code) {
const url = new URL(process.env.OAUTH_AUTHORIZE_URL);
url.searchParams.set('client_id', process.env.OAUTH_CLIENT_ID);

return response.redirect(url.toString());
}

return this.authService.oAuthGithub(code, response);
}
}
12 changes: 11 additions & 1 deletion backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { JwtService } from '@nestjs/jwt';
import axios from 'axios';
import * as bcrypt from 'bcrypt';
import { generate } from 'generate-password';
import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry';
import { UsersService } from '../users/users.service';
// import { SentryService } from '../sentry/sentry.service';

@Injectable()
export class AuthService {
constructor(
private usersService: UsersService,
private jwtService: JwtService,
@InjectSentry() private readonly sentryService: SentryService,
) {}

async validateUser(email: string, pass: string): Promise<any> {
Expand Down Expand Up @@ -50,6 +53,8 @@ export class AuthService {
headers: { Authorization: `Bearer ${parsedData.access_token}` },
});

this.sentryService.debug(`github user email: ${githubUserData.email}`);

let user = githubUserData.email
? await this.usersService.findByEmail(githubUserData.email)
: null;
Expand All @@ -65,6 +70,11 @@ export class AuthService {
user = await this.usersService.create(userDto);
}

return this.login(user, response);
this.sentryService.debug(`current user email: ${user.email}`);

const payload = { email: user.email, sub: user.id };
const token = this.jwtService.sign(payload);
response.cookie('access_token', token);
return response.redirect('/profile');
}
}
12 changes: 8 additions & 4 deletions backend/src/config/sentry.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { LogLevel } from '@nestjs/common';

export default () => {
const settings = {
dsn: process.env.SENTRY_DSN,
tracesSampleRate: null,
debug: true,
environment: process.env.NODE_ENV || 'dev',
logLevels: [<LogLevel>'debug'], // based on sentry.io loglevel //
};
if (process.env.NODE_ENV === 'production') {
settings.tracesSampleRate = 1.0;
}
// if (process.env.NODE_ENV === 'production') {
// settings.tracesSampleRate = 1.0;
// }

return settings;
};
4 changes: 4 additions & 0 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import * as cookieParser from 'cookie-parser';
import { useContainer } from 'class-validator';
import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { SentryService } from '@ntegral/nestjs-sentry';
import { AppModule } from './app.module';

declare const module: any;

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);

app.useLogger(SentryService.SentryServiceInstance());

app.setGlobalPrefix('api');
app.enable('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
app.use(cookieParser());
Expand Down
41 changes: 0 additions & 41 deletions backend/src/sentry/sentry.interceptor.ts

This file was deleted.

33 changes: 0 additions & 33 deletions backend/src/sentry/sentry.module.ts

This file was deleted.

55 changes: 0 additions & 55 deletions backend/src/sentry/sentry.service.ts

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@monaco-editor/react": "^4.4.6",
"@reduxjs/toolkit": "^1.9.3",
"@sentry/react": "^7.38.0",
"@sentry/react": "^7.47.0",
"@sentry/tracing": "^7.38.0",
"axios": "^1.3.3",
"bootstrap": "^5.2.3",
Expand Down
22 changes: 2 additions & 20 deletions frontend/src/Pages/SignIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from 'axios';
import { Button, Card, Col, Container, Form, Row } from 'react-bootstrap';
import { object, string } from 'yup';
import { useFormik } from 'formik';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useAuth } from '../hooks';

Expand All @@ -18,7 +18,6 @@ function SignIn() {
const navigate = useNavigate();
const { t } = useTranslation();
const auth = useAuth();
const [searchParams] = useSearchParams();

const validation = object().shape({
email: string()
Expand Down Expand Up @@ -60,24 +59,7 @@ function SignIn() {
});

useEffect(() => {
const code = searchParams.get('code');
if (!code) {
inputRef?.current?.focus();
return;
}

formik.setSubmitting(true);

const oAuth = async () => {
try {
await auth.oAuth(code);
auth.logIn();
navigate(routes.profilePagePath());
} catch (e) {
formik.setSubmitting(false);
}
};
oAuth();
inputRef?.current?.focus();
}, []);

return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/application.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default async () => {
});

Sentry.init({
dsn: process.env.SENTRY_DSN,
dsn: process.env.REACT_APP_SENTRY_DSN,
integrations: [new BrowserTracing()],
tracesSampleRate: 1.0,
});
Expand Down
14 changes: 1 addition & 13 deletions frontend/src/providers/AuthProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,6 @@ function AuthProvider({ children }) {
setLoggedIn(true);
};

const getOAuthCodeUrl = () => {
const url = new URL(process.env.REACT_APP_OAUTH_AUTHORIZE_URL);
url.searchParams.set('client_id', process.env.REACT_APP_OAUTH_CLIENT_ID);

return url.toString();
};

const oAuth = async (code) => {
const response = await axios.post(routes.oAuthPath(), { code });
console.log('data: ', response);
};

useEffect(() => {
const fetchAuthData = async () => {
try {
Expand All @@ -49,7 +37,7 @@ function AuthProvider({ children }) {
}, []);

const memoizedValue = useMemo(
() => ({ logOut, isLoggedIn, logIn, oAuth, getOAuthCodeUrl }),
() => ({ logOut, isLoggedIn, logIn }),
[logOut, isLoggedIn, logIn],
);

Expand Down
Loading

0 comments on commit 351bf4b

Please sign in to comment.