Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add authentication #1807

Merged
merged 1 commit into from
Jul 23, 2023
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
46 changes: 46 additions & 0 deletions server/src/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Request, Response, NextFunction } from 'express'
const clientId = process.env.CLIENT_ID
const clientSecret = process.env.CLIENT_SECRET

export const checkAuth = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { data } = req.query

if (!data) {
return res.status(400).json({ success: false, message: 'data not found' })
}

try {
const tokenResponse = await fetch(
'https://github.com/login/oauth/access_token',
{
method: 'POST',
headers: { Accept: 'application/json' },
body: JSON.stringify({
client_id: clientId,
client_secret: clientSecret,
code: data,
}),
}
).then((res) => res.json())

const accessToken = tokenResponse.data.access_token

const userResponse = await fetch('https://api.github.com/user', {
method: 'GET',
headers: { Authorization: `token ${accessToken}` },
}).then((res) => res.json())

// const userData = userResponse.data

// req.session.user = userData

next()
} catch (error) {
console.error('GitHub OAuth Error:', error)
return res.status(500).json({ success:false,message: 'GitHub authentication failed' })
}
}
1 change: 1 addition & 0 deletions server/src/projects/projects.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ProjectModel } from '../schema/Projects/project.schema'
import { AllProjectsType } from './project.interface'

const getAllProjects = async () => {
Expand Down
1 change: 1 addition & 0 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserModel } from "../schema/User/user.schema"
import { UserType } from "./user.interface"


Expand Down
Loading