Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
8fb20c3
Create route for testing password
pernillasterner May 22, 2024
366e8a8
Merge pull request #1 from pernillasterner/feature-initial-setup
pernillasterner May 22, 2024
047e5de
Ignore note file
pernillasterner May 22, 2024
2f68fd1
Add registration form
pernillasterner May 22, 2024
5ccdfda
Add components to register and log in
pernillasterner May 22, 2024
4c45442
Creating endpoints for registration
pernillasterner May 22, 2024
4809d28
Merge pull request #2 from pernillasterner/feature-middleware
pernillasterner May 22, 2024
1e3d2f6
Feature team collaboration backend frontend (#3)
pernillasterner May 22, 2024
c635550
Feature routing (#4)
pernillasterner May 22, 2024
7e7010f
Styling (#5)
pernillasterner May 23, 2024
9e4ccdf
Notes added
fridaforser May 23, 2024
3e85ba7
Added IsLoggedIn-state + Header changes depending on IsLoggedIn
Tejpex May 23, 2024
56ed7b5
Merge pull request #6 from pernillasterner/dynamic-header
Tejpex May 23, 2024
897b74f
Styling
fridaforser May 23, 2024
2312304
Merge pull request #7 from pernillasterner/Styling
fridaforser May 23, 2024
4f3b9f7
Change post to get for secret message and play is checking for logged…
Tejpex May 23, 2024
54834a0
Styling for game cards
fridaforser May 23, 2024
ec5d295
Merge pull request #8 from pernillasterner/styling
fridaforser May 23, 2024
bcff91f
Feature deploy render mongodb (#9)
pernillasterner May 23, 2024
e5952d1
Add npm run dev
pernillasterner May 23, 2024
e5cbe36
Remove npm run dev
pernillasterner May 23, 2024
f380292
dependencies for deploy
fridaforser May 23, 2024
59117ca
Merge pull request #10 from pernillasterner/styling
fridaforser May 23, 2024
8d9898d
dependencies
fridaforser May 23, 2024
c821c07
Merge pull request #11 from pernillasterner/styling
fridaforser May 23, 2024
1593004
Remove dotenv
pernillasterner May 23, 2024
0783330
test to deploy
fridaforser May 23, 2024
d7064b9
Merge pull request #12 from pernillasterner/styling
fridaforser May 23, 2024
1341952
Delete netlify.toml
fridaforser May 23, 2024
a5ebd38
added endpoints to server
fridaforser May 23, 2024
c6a9864
Merge pull request #13 from pernillasterner/styling
fridaforser May 23, 2024
01ff160
Add dotenv
pernillasterner May 23, 2024
5d88911
Update url to render url
pernillasterner May 23, 2024
19479e7
Testing fetch respons with new url
pernillasterner May 24, 2024
e53a4a2
Redo changes
pernillasterner May 24, 2024
851c368
Add /users to url
pernillasterner May 24, 2024
6f6db9d
Rendo endpoint
pernillasterner May 24, 2024
09c16b4
Add /users to url
pernillasterner May 24, 2024
de13424
Redo users endpoint
pernillasterner May 24, 2024
e572033
Add users to endpoint
pernillasterner May 24, 2024
e5b02ec
Save accesstoken in localstorage
pernillasterner May 24, 2024
5a43ac0
Testing db connection
pernillasterner May 24, 2024
c13c0d4
Testing login func
pernillasterner May 24, 2024
04db0d6
Change navigation to play after login
pernillasterner May 24, 2024
2d80879
Add comments
pernillasterner May 24, 2024
faf3007
Merge pull request #14 from pernillasterner/secret-game-page
Tejpex May 24, 2024
9b862e2
Added fetch to play-site
Tejpex May 24, 2024
7fcac3c
Merge pull request #15 from pernillasterner/secret-page
Tejpex May 24, 2024
ea7f027
Added styling
fridaforser May 24, 2024
4c2d6af
sign out functionality
fridaforser May 24, 2024
5ce585c
Merge pull request #17 from pernillasterner/Sign-out
fridaforser May 24, 2024
d371819
small styling edit
fridaforser May 24, 2024
c655845
Merge pull request #18 from pernillasterner/fixes-in-styling
fridaforser May 24, 2024
0c4b4fa
Install useForm hook to reduce code and make validation easier to han…
pernillasterner May 24, 2024
7fe0cb5
added 401 and 403 error messages for the authenticated endpoint
May 26, 2024
3a5f00a
Cleaned up code
Tejpex May 26, 2024
3916cb4
Put note.md in git-ignore
Tejpex May 26, 2024
6b47dc2
Removed console-logs
Tejpex May 26, 2024
2e92736
Update with description and deployment links
pernillasterner May 26, 2024
a3a0fe7
Add contributers
pernillasterner May 26, 2024
5ef5db9
Checking for connection to render
Tejpex May 26, 2024
cdfd09d
Added status-codes + Secret message
Tejpex May 26, 2024
f71c1b1
Update server.js
May 27, 2024
c773f97
Worked with debugging
Tejpex May 27, 2024
eb3d913
Merge pull request #20 from pernillasterner/checking-endpoints
Tejpex May 27, 2024
05f1309
Update README.md
pernillasterner May 31, 2024
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

package-lock.json
package-lock.json
note.md
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# Project Auth API

Replace this readme with your own information about your project.

Start by briefly describing the assignment in a sentence or two. Keep it short and to the point.
For this project, the focus was on connecting the frontend and backend by creating a registration form that not only registers the user but also logs them in and returns an access token used to authenticate the user. Since we are developing an application for children aged 7-9 years old to help them improve their learning skills in English, Swedish, and math, we wanted to implement these functionalities in this project using MongoDB as the database, and ReactJS and ExpressJS to handle the frontend and fetch data from the API. In this project, we are able to register, log in, and log out a user.

## The problem

Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next?
We had some problems deploying both the frontend and backend, but we worked together and solved them. We also found some difficulty adding the user data to the database after deployment. However, after adding the correct endpoints and using the correct connection string to MongoDB, we were able to start registering new users and use that to log i

## View it live

Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about.
[Frontend](https://technigo-project-auth.netlify.app/)

[Backend](https://technigo-project-auth.onrender.com/)

## Contributors

Thanks to these great people for contributing to this project:

- [@Tejpex](https://github.com/Tejpex)
- [@fridaforser](https://github.com/fridaforser)
- [@KatSjolin](https://github.com/KatSjolin)
7 changes: 6 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"bcrypt-nodejs": "^0.0.3",
"cors": "^2.8.5",
"express": "^4.17.3",
"mongoose": "^8.0.0",
"mongodb": "^6.6.2",
"mongoose": "^8.4.0",
"nodemon": "^3.0.1"
},
"devDependencies": {
"@types/bcrypt-nodejs": "^0.0.31"
}
}
136 changes: 127 additions & 9 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,142 @@
import cors from "cors";
import express from "express";
import mongoose from "mongoose";
import bcrypt from "bcrypt-nodejs";
import crypto from "crypto";

const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/project-mongo";
// Defining port and connecting to mongoose
const port = process.env.PORT || 8000;
const app = express();
const mongoUrl = process.env.MONGO_URL || "mongodb://localhost/auth";

console.log("Connecting to MongoDB at:", mongoUrl);
mongoose.connect(mongoUrl);
mongoose.Promise = Promise;

// Defines the port the app will run on. Defaults to 8080, but can be overridden
// when starting the server. Example command to overwrite PORT env variable value:
// PORT=9000 npm start
const port = process.env.PORT || 8080;
const app = express();
// Defining schema for a User
const User = mongoose.model("User", {
username: {
type: String,
unique: true,
required: true,
},
password: {
type: String,
required: true,
},
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
age: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
accessToken: {
type: String,
default: () => crypto.randomBytes(128).toString("hex"),
},
});

// Add middlewares to enable cors and json body parsing
//Authenticate user as middleware
const authenticateUser = async (req, res, next) => {
const accessToken = req.header("Authorization");
if (!accessToken) {
return res.status(401).json({ error: "Unauthorized: Missing access token" });
}

const user = await User.findOne({ accessToken })
if (user) {
console.log("User is found", user);
req.user = user;
next();
} else {
return res.status(403).json({ error: "Forbidden: Invalid access token" });
}
};

// Middlewares to enable cors and json body parsing
app.use(cors());
app.use(express.json());
app.use((req, res, next) => {
if (mongoose.connection.readyState === 1) {
next();
} else {
res.status(503).json({ error: "Service unavailable." });
}
});

// Start defining your routes here
// Defining routes
app.get("/", (req, res) => {
res.send("Hello Technigo!");
res.send("Hello friend!");
});

//User-endpoint for developing purpose
app.get("/users", async (req, res) => {
const allUsers = await User.find().exec();
if (allUsers.length > 0) {
res.status(200).json(allUsers);
} else {
res.status(404).send("No users found");
}
});

//Create user with username, password etc.
app.post("/users", async (req, res) => {
try {
const { username, firstName, lastName, age, email, password } = req.body;
const salt = bcrypt.genSaltSync(10);
const user = new User({
username,
firstName,
lastName,
age,
email,
password: bcrypt.hashSync(password, salt),
});
await user.save();
res.status(201).json({ id: user._id, accessToken: user.accessToken });
} catch (error) {
res
.status(400)
.json({ response: error, message: "Could not create user." });
}
});

//Endpoint for login
app.post("/sessions", async (req, res) => {
const userByUsername = await User.findOne({ username: req.body.username });
const userByEmail = await User.findOne({ email: req.body.email });
if (
userByUsername &&
bcrypt.compareSync(req.body.password, userByUsername.password)
) {
res.status(200).json({userId: userByUsername._id, accessToken: userByUsername.accessToken,
});
} else if (
userByEmail &&
bcrypt.compareSync(req.body.password, userByEmail.password)
) {
res.status(200).json({ userId: userByEmail._id, accessToken: userByEmail.accessToken });
} else {
res.status(404).json({ notFound: true });
}
});

app.get("/games", authenticateUser);
app.get("/games", async (req, res) => {
res
.status(200)
.json({ message: "Secret message only for logged in users to see!" });
});

// Start the server
Expand Down
5 changes: 4 additions & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Jost:ital,wght@0,100..900;1,100..900&family=Kumbh+Sans:wght@100..900&family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&family=Manrope:wght@200..800&family=Montserrat:ital,wght@0,100..900;1,100..900&family=Nunito:ital,wght@0,200..1000;1,200..1000&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Quicksand:wght@300..700&display=swap" rel="stylesheet">
<title>Pluggin</title>
</head>
<body>
<div id="root"></div>
Expand Down
7 changes: 6 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
"preview": "vite preview"
},
"dependencies": {
"dotenv": "^16.4.5",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-hook-form": "^7.51.5",
"react-router": "^6.23.1",
"react-router-dom": "^6.23.1",
"styled-components": "^6.1.11"
},
"devDependencies": {
"@types/react": "^18.2.15",
Expand Down
32 changes: 31 additions & 1 deletion frontend/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import { BrowserRouter, Routes, Route } from "react-router-dom";
import { Header } from "./components/Header";
import { RegistrationForm } from "./components/RegistrationForm";
import { Login } from "./components/Login";
import { Play } from "./components/Play";
import { Math } from "./components/Games/Math";
import { Swedish } from "./components/Games/Swedish";
import { English } from "./components/Games/English";
import { Progress } from "./components/MyProgress";
import { WelcomeText } from "./components/WelcomeText";
import { UserProvider } from "./contexts/UserContext";

export const App = () => {
return <div>Find me in src/app.jsx!</div>;
return (
<BrowserRouter>
<UserProvider>
<Header />
<main>
<Routes>
<Route path="/" element={<WelcomeText />} />
<Route path="/register" element={<RegistrationForm />} />
<Route path="/login" element={<Login />} />
<Route path="/play" element={<Play />} />
<Route path="/play/math" element={<Math />} />
<Route path="/play/swedish" element={<Swedish />} />
<Route path="/play/english" element={<English />} />
<Route path="/myprogress" element={<Progress />} />
</Routes>
</main>
</UserProvider>
</BrowserRouter>
);
};
1 change: 0 additions & 1 deletion frontend/src/assets/react.svg

This file was deleted.

Binary file added frontend/src/assets/signouticon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions frontend/src/components/Button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import styled from "styled-components";

export const Button = styled.button`
background-color: var(--raspberry);
color: #fff;
border-radius: 30px;
border: none;
padding: 10px 20px;
font-size: 16px;
font-weight: 500;
transition: 0.2s ease;
text-decoration: none;
margin-top: 20px;

&:hover {
background-color: var(--raspberryhover);
cursor: pointer;
}

&:active {
background-color: var(--raspberryactive);
}
`;

export default Button;
8 changes: 8 additions & 0 deletions frontend/src/components/Games/English.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styled from "styled-components";

export const English = () => {
return <EnglishTitle>Let&apos;s play an English game!</EnglishTitle>;
};
const EnglishTitle = styled.h2`
color: black;
`;
8 changes: 8 additions & 0 deletions frontend/src/components/Games/Math.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styled from "styled-components";

export const Math = () => {
return <MathTitle>Let&apos;s play a math game!</MathTitle>;
};
const MathTitle = styled.h2`
color: black;
`;
8 changes: 8 additions & 0 deletions frontend/src/components/Games/Swedish.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import styled from "styled-components";

export const Swedish = () => {
return <SwedishTitle>Let&apos;s play a Swedish game!</SwedishTitle>;
};
const SwedishTitle = styled.h2`
color: black;
`;
Loading