Skip to content

Commit

Permalink
feat: save login user to db EddieHubCommunity#4767 (EddieHubCommunity…
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiejaoude authored Feb 13, 2023
1 parent 063e7d6 commit 41c01a7
Show file tree
Hide file tree
Showing 8 changed files with 4,744 additions and 2,078 deletions.
9 changes: 6 additions & 3 deletions config/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from "fs";

import logger from "../config/logger";

let hasConnection = false;
let hasConnection;
const connectMongo = async () => {
if (!process.env.LINKFREE_MONGO_CONNECTION_STRING) {
throw new Error(
Expand All @@ -12,7 +12,7 @@ const connectMongo = async () => {
}

if (hasConnection) {
return;
return hasConnection;
}
try {
// DigitalOcean Apps has cert as environment variable but Mongo needs a file path
Expand All @@ -21,9 +21,12 @@ const connectMongo = async () => {
fs.writeFileSync("cert.pem", process.env.CA_CERT);
}

await mongoose.connect(process.env.LINKFREE_MONGO_CONNECTION_STRING);
hasConnection = await mongoose.connect(
process.env.LINKFREE_MONGO_CONNECTION_STRING
);
hasConnection = true;
logger.info("MongoDB connected");
return hasConnection;
} catch (e) {
hasConnection = false;
logger.error(e, "DB connection failed");
Expand Down
59 changes: 59 additions & 0 deletions models/Account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import mongoose from "mongoose";

const AccountSchema = new mongoose.Schema({
userId: {
type: String,
trim: true,
},
type: {
type: String,
trim: true,
},
provider: {
type: String,
trim: true,
},
providerAccountId: {
type: String,
trim: true,
},
refresh_token: {
type: String,
trim: true,
},
access_token: {
type: String,
trim: true,
},
expires_at: {
type: Number,
trim: true,
},
token_type: {
type: String,
trim: true,
},
scope: {
type: String,
trim: true,
},
id_token: {
type: String,
trim: true,
},
session_state: {
type: String,
trim: true,
},
oauth_token_secret: {
type: String,
trim: true,
},
oauth_token: {
type: String,
trim: true,
},
});

module.exports =
mongoose.models.Account || mongoose.model("Account", AccountSchema);
19 changes: 19 additions & 0 deletions models/Session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose from "mongoose";

const SessionSchema = new mongoose.Schema({
expires: {
type: Date,
trim: true,
},
sessionToken: {
type: String,
trim: true,
},
userId: {
type: String,
ref: "User",
},
});

module.exports =
mongoose.models.Session || mongoose.model("Session", SessionSchema);
22 changes: 22 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import mongoose from "mongoose";

const UserSchema = new mongoose.Schema({
name: {
type: String,
trim: true,
},
email: {
type: String,
trim: true,
},
emailVerified: {
type: Date,
trim: true,
},
image: {
type: String,
trim: true,
},
});

module.exports = mongoose.models.User || mongoose.model("User", UserSchema);
20 changes: 20 additions & 0 deletions models/VerificationToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mongoose from "mongoose";

const VerificationTokenSchema = new mongoose.Schema({
expires: {
type: Date,
trim: true,
},
token: {
type: String,
trim: true,
},
identifier: {
type: String,
trim: true,
},
});

module.exports =
mongoose.models.VerificationToken ||
mongoose.model("VerificationToken", VerificationTokenSchema);
Loading

0 comments on commit 41c01a7

Please sign in to comment.