Skip to content

Commit 7ba6fb4

Browse files
committed
Google and GitHub OAuth2 in Node.js
0 parents  commit 7ba6fb4

File tree

23 files changed

+1923
-0
lines changed

23 files changed

+1923
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
# Keep environment variables out of version control
3+
.env

example.env

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DATABASE_URL="file:./dev.db"
2+
3+
GOOGLE_OAUTH_CLIENT_ID=
4+
GOOGLE_OAUTH_CLIENT_SECRET=
5+
GOOGLE_OAUTH_REDIRECT=http://localhost:8000/api/sessions/oauth/google
6+
7+
GITHUB_OAUTH_CLIENT_ID=
8+
GITHUB_OAUTH_CLIENT_SECRET=
9+
GITHUB_OAUTH_REDIRECT_URL=http://localhost:8000/api/sessions/oauth/github
10+
11+
NODE_ENV=development
12+
JWT_SECRET=my_ultra_secure_secret
13+
TOKEN_EXPIRES_IN=60
14+
FRONTEND_ORIGIN=http://localhost:3000

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "google-github-oauth2-nodejs",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"start": "ts-node-dev --respawn --transpile-only src/app.ts",
8+
"db:migrate": "npx prisma migrate dev --name 'users' --create-only",
9+
"db:generate": " npx prisma generate",
10+
"db:push": "npx prisma db push"
11+
},
12+
"devDependencies": {
13+
"@types/cookie-parser": "^1.4.3",
14+
"@types/cors": "^2.8.13",
15+
"@types/express": "^4.17.15",
16+
"@types/jsonwebtoken": "^9.0.1",
17+
"@types/morgan": "^1.9.4",
18+
"@types/node": "^18.11.18",
19+
"@types/qs": "^6.9.7",
20+
"morgan": "^1.10.0",
21+
"prisma": "^4.8.1",
22+
"ts-node-dev": "^2.0.0",
23+
"typescript": "^4.9.4"
24+
},
25+
"dependencies": {
26+
"@prisma/client": "^4.8.1",
27+
"axios": "^1.2.2",
28+
"cookie-parser": "^1.4.6",
29+
"cors": "^2.8.5",
30+
"dotenv": "^16.0.3",
31+
"express": "^4.18.2",
32+
"jsonwebtoken": "^9.0.0",
33+
"qs": "^6.11.0",
34+
"zod": "^3.20.2"
35+
}
36+
}

prisma/dev.db

24 KB
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- CreateTable
2+
CREATE TABLE "User" (
3+
"id" TEXT NOT NULL PRIMARY KEY,
4+
"name" TEXT NOT NULL,
5+
"email" TEXT NOT NULL,
6+
"password" TEXT NOT NULL,
7+
"role" TEXT NOT NULL DEFAULT 'user',
8+
"photo" TEXT NOT NULL DEFAULT 'default.png',
9+
"verified" BOOLEAN NOT NULL DEFAULT false,
10+
"provider" TEXT NOT NULL DEFAULT 'local',
11+
"createdAt" DATETIME NOT NULL,
12+
"updatedAt" DATETIME NOT NULL
13+
);
14+
15+
-- CreateIndex
16+
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the `User` table. If the table is not empty, all the data it contains will be lost.
5+
6+
*/
7+
-- DropTable
8+
PRAGMA foreign_keys=off;
9+
DROP TABLE "User";
10+
PRAGMA foreign_keys=on;
11+
12+
-- CreateTable
13+
CREATE TABLE "users" (
14+
"id" TEXT NOT NULL PRIMARY KEY,
15+
"name" TEXT NOT NULL,
16+
"email" TEXT NOT NULL,
17+
"password" TEXT NOT NULL,
18+
"role" TEXT NOT NULL DEFAULT 'user',
19+
"photo" TEXT NOT NULL DEFAULT 'default.png',
20+
"verified" BOOLEAN NOT NULL DEFAULT false,
21+
"provider" TEXT NOT NULL DEFAULT 'local',
22+
"createdAt" DATETIME NOT NULL,
23+
"updatedAt" DATETIME NOT NULL
24+
);
25+
26+
-- CreateIndex
27+
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

prisma/migrations/migration_lock.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "sqlite"

prisma/schema.prisma

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
generator client {
5+
provider = "prisma-client-js"
6+
}
7+
8+
datasource db {
9+
provider = "sqlite"
10+
url = env("DATABASE_URL")
11+
}
12+
13+
model User {
14+
id String @id @default(uuid())
15+
name String
16+
email String @unique
17+
password String
18+
role String @default("user")
19+
photo String @default("default.png")
20+
verified Boolean @default(false)
21+
provider String @default("local")
22+
23+
createdAt DateTime
24+
updatedAt DateTime @updatedAt
25+
26+
@@map(name: "users")
27+
}

public/default.png

18.6 KB
Loading

src/app.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require("dotenv").config();
2+
import path from "path";
3+
import express, { NextFunction, Request, Response } from "express";
4+
import morgan from "morgan";
5+
import cors from "cors";
6+
import cookieParser from "cookie-parser";
7+
import userRouter from "./routes/user.route";
8+
import authRouter from "./routes/auth.route";
9+
import sessionRouter from "./routes/session.route";
10+
import connectDB from "./utils/prisma";
11+
12+
const app = express();
13+
14+
app.use(express.json({ limit: "10kb" }));
15+
app.use(cookieParser());
16+
if (process.env.NODE_ENV === "development") app.use(morgan("dev"));
17+
app.use("/api/images", express.static(path.join(__dirname, "../public")));
18+
19+
const FRONTEND_ORIGIN = process.env.FRONTEND_ORIGIN as unknown as string;
20+
app.use(
21+
cors({
22+
credentials: true,
23+
origin: [FRONTEND_ORIGIN],
24+
})
25+
);
26+
27+
app.use("/api/users", userRouter);
28+
app.use("/api/auth", authRouter);
29+
app.use("/api/sessions", sessionRouter);
30+
31+
app.get("/api/healthChecker", (req: Request, res: Response) => {
32+
res.status(200).json({
33+
status: "success",
34+
message: "Implement OAuth in Node.js",
35+
});
36+
});
37+
38+
// UnKnown Routes
39+
app.all("*", (req: Request, res: Response, next: NextFunction) => {
40+
const err = new Error(`Route ${req.originalUrl} not found`) as any;
41+
err.statusCode = 404;
42+
next(err);
43+
});
44+
45+
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
46+
err.status = err.status || "error";
47+
err.statusCode = err.statusCode || 500;
48+
49+
res.status(err.statusCode).json({
50+
status: err.status,
51+
message: err.message,
52+
});
53+
});
54+
55+
const port = 8000;
56+
app.listen(port, () => {
57+
console.log(`✅ Server started on port: ${port}`);
58+
connectDB();
59+
});

0 commit comments

Comments
 (0)