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

Swift #6

Merged
merged 15 commits into from
Sep 1, 2022
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
Binary file added .DS_Store
Binary file not shown.
24 changes: 15 additions & 9 deletions API/client/user.http
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@ POST https://localhost:8081/signUp HTTP/1.1
Content-Type: application/json

{
"firstname": "Yanis",
"phonenumber": "0606060606",
"lastname": "Wukong",
"pseudo": "SunWukong",
"email": "YanisWukong@gmail.com",
"password": "123456789Wu"
"firstname": "test",
"phonenumber": "0603060607",
"lastname": "test",
"pseudo": "test",
"email": "test@gmail.com",
"password": "test"
}

###
POST https://localhost:8081/login HTTP/1.1
Content-Type: application/json

{
"email": "gkata@gmail.com",
"password": "123456"
"email": "test@gmail.com",
"password": "test"
}

###
POST https://localhost:8081/logout HTTP/1.1
Content-Type: application/json
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJlYmJlZGZhMmRiNGFiOTRmODUzMDg4IiwiZW1haWwiOiJna2F0YUBnbWFpbC5jb20iLCJpYXQiOjE2NTk2MjE0MTksImV4cCI6MTY1OTY0NjYxOX0.QA1XQUwq8T8awnI6TYJz1_-Srwsn-SpNdQturTwKYa4
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjJlNzk3ODM2MTUwMTUxYzUwYjI0ZGM4IiwiZW1haWwiOiJzb25nb2t1QGdtYWlsLmNvbSIsImlhdCI6MTY1OTM0NTA1NywiZXhwIjoxNjU5MzcwMjU3fQ.PAk9FH0W8jw9HxAvhxoSeDinVEV1M3h1zxvixueajA8

###
GET https://localhost:8081/getProfile HTTP/1.1
Content-Type: application/json
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNjMwYTNkNGNlN2ZhNjM5Yjk5NWE0NTVmIiwiZW1haWwiOiJ0ZXN0QGdtYWlsLmNvbSIsImlhdCI6MTY2MTYyMzMyMSwiZXhwIjoxNjYxNjQ4NTIxfQ.AgvF4-kKm2Qb4jbZG8nInzPyL6Hb6PApPD0FiQkw-w4

30 changes: 26 additions & 4 deletions API/controller/user/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const signUp = async (body,res) => {
newUser.password = encryptedPassword
newUser.fk_role = Mongoose.Types.ObjectId("62e436aa1a254799431166b0")
await newUser.save()
res.status(200).json(newUser)
res.status(200).json({message: "User created"})
}
}
}
Expand All @@ -36,7 +36,9 @@ const login = async(body,res) => {
res.status(400).send("All input is required");
}
// Find if user exist
console.log(email)
const user = await UserModel.findOne({email: email });
console.log(user)
const RoleUser = await RoleModel.findOne({_id: user.fk_role})
//if my user exist and the password match
if (user && (await bcrypt.compare(password, user.password)) && RoleUser.name == "user") {
Expand All @@ -54,7 +56,7 @@ const login = async(body,res) => {
user.save()

// user
res.status(200).json(user.token);
res.status(200).json({message: user.token});
}else {
res.status(400).send("Invalid Credentials")
}
Expand All @@ -68,11 +70,31 @@ const logout = async(body, res) => {
const user = await UserModel.findOne({email})
user.token = ""
if(user.save()){
res.status(200).send("you are disconnected")
res.status(200).send({message: "Successfully logged out"})
}else {
res.status(400).send("error in the process of logout")
}
}
}

module.exports = {signUp, login, logout}
const getProfile = async(req, res) => {
const token = req.headers["authorization"];
if (token) {
const user = await UserModel.findOne({token})
if (!user) {
return res.status(403).send("Something wrong with your request");
}
return res.status(200).json({
id: user._id,
email: user.email,
lastname: user.lastname,
firstname: user.firstname,
pseudo: user.pseudo,
phone: user.phonenumber,
})
} else {
return res.status(403).send("You need a token");
}
}

module.exports = {signUp, login, logout, getProfile}
4 changes: 4 additions & 0 deletions API/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ router.post('/logout', isAuthorized, async (req, res) => {
await auth.logout(req, res)
})

router.get('/getProfile', isAuthorized, async(req, res) => {
await auth.getProfile(req,res)
})


// Define the User schema in the swagger, we will use it in endpoints descriptions

Expand Down
Binary file added CoWorkApp/.DS_Store
Binary file not shown.
Loading