Skip to content

Commit

Permalink
added auth and mongodb integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurell876 committed May 26, 2020
1 parent 960d2c8 commit 1bae7d5
Show file tree
Hide file tree
Showing 15 changed files with 2,974 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.fontFamily": "Consolas,'OperatorMono-Book', monospace",
"editor.fontLigatures":
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"# tenten-api-rework"
database credentials are in nodemon.json file
39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { ApolloServer, gql } = require("apollo-server");
const mongoose = require("mongoose");
const Query = require("./resolvers/Query");
const Mutation = require("./resolvers/Mutation");
const typeDefs = require("./schema.graphql");
const isAuth = require("./middleware/is-auth");
const User = require("./resolvers/User")
const express = require("express");
const app = express();

const resolvers = {
Query,
Mutation,
User
};

const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => {
return isAuth(req)
},
});


mongoose
.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@talawa-dev-nk4oo.mongodb.net/${process.env.MONGO_DB}?retryWrites=true&w=majority`,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => {
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
})
.catch((e) => console.log(e));
59 changes: 59 additions & 0 deletions middleware/is-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const jwt = require("jsonwebtoken");

const isAuth = (req) => {
//This checks to see if there is an authorization field within the incoming request
const authHeader = req.headers.authorization

//if there is no token
if (!authHeader) {
const isAuth = false;
return {
isAuth
};
}

//format of request sent will be Bearer tokenvalue
//this splits it into two values bearer and the token
const token = authHeader.split(" ")[1];

//if the token is null or an empty string
if (!token || token === "") {
const isAuth = false;
return {
isAuth
};
}

//uses key created in the auth resolver
//to be changed in production
//only tokens created with this key will be valid tokens
let decodedToken;
try {
decodedToken = jwt.verify(token, "somesupersecretkey");
} catch (e) {
const isAuth = false;
return {
isAuth
};
}

//if the decoded token is not set
if (!decodedToken) {
const isAuth = false;
return {
isAuth
};
}

//shows the user is an authenticated user
const isAuth = true;
//pulls data off the token
const userId = decodedToken.userId;

return {
isAuth,
userId
}
}

module.exports = isAuth
34 changes: 34 additions & 0 deletions models/Organization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const organizationSchema = new Schema({
name: {
type:String,
required:true
},
description: {
type:String,
required:true
},
creator: {
type: Schema.Types.ObjectId,
ref: "User",
required:true
},
members: [
{
type: Schema.Types.ObjectId,
ref: "User"
}
],
admins: [
{
type: Schema.Types.ObjectId,
ref: "User",
required:true
}
]
})

module.exports = mongoose.model("Organization", organizationSchema);
45 changes: 45 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const mongoose = require("mongoose");
const { isEmail } = require('validator');
const Schema = mongoose.Schema;


const userSchema = new Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
validate: [ isEmail, 'invalid email' ],
required: true
},
password: {
type: String,
required: true
},
createdOrganizations: [
{
type: Schema.Types.ObjectId,
ref: "Organization"
}
],
joinedOrganizations: [
{
type: Schema.Types.ObjectId,
ref: "Organization"
}
],
adminFor: [
{
type: Schema.Types.ObjectId,
ref: "Organization"
}
]
})

module.exports = mongoose.model("User", userSchema);

7 changes: 7 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env" : {
"MONGO_USER":"devtest876",
"MONGO_PASSWORD":"DGbtoevKIx1Wajvx",
"MONGO_DB":"talawa-dev"
}
}
Loading

0 comments on commit 1bae7d5

Please sign in to comment.