201 lines
5.2 KiB
JavaScript
201 lines
5.2 KiB
JavaScript
const {Application} = require("../core/server");
|
||
const Joi = require("joi");
|
||
const { PostDataProcess } = require("../core/postdata");
|
||
const User = require("../database/user");
|
||
|
||
|
||
Application.get("/", LoginPage);
|
||
Application.get("/login", LoginPage);
|
||
Application.get("/register", RegisterPage);
|
||
|
||
|
||
Application.post("/login", PostDataProcess(), Login);
|
||
Application.post("/register", PostDataProcess(), Register);
|
||
Application.get("/logout", MiddlewareAuth, Logout);
|
||
|
||
/**
|
||
* @param {import("express").Request} request
|
||
* @param {import("express").Response} response
|
||
*/
|
||
async function LoginPage(request, response)
|
||
{
|
||
if(typeof request.session.authendicated == "boolean" && request.session.authendicated == true)
|
||
{
|
||
response.redirect(307,"/panel");
|
||
}else{
|
||
response.render("login");
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @param {import("express").Request} request
|
||
* @param {import("express").Response} response
|
||
*/
|
||
async function RegisterPage(request, response)
|
||
{
|
||
response.render("register");
|
||
}
|
||
|
||
|
||
/**
|
||
* @param {import("express").Request} request
|
||
* @param {import("express").Response} response
|
||
*/
|
||
async function Register(request, response)
|
||
{
|
||
const error = registerValidation(request.body);
|
||
|
||
if(error)
|
||
{
|
||
return response.status(400).json({
|
||
status: "fail",
|
||
message: error.message
|
||
});
|
||
}
|
||
|
||
if(await User.hasUser(request.body.email))
|
||
{
|
||
return response.status(400).json({
|
||
status: "fail",
|
||
message: "E-Mail adresi zaten kullanılıyor."
|
||
});
|
||
}
|
||
|
||
let userid;
|
||
try{
|
||
userid = await User.createUser(
|
||
request.body.name,
|
||
request.body.surname,
|
||
request.body.email,
|
||
request.body.password
|
||
);
|
||
}catch{
|
||
return response.status(500).json({
|
||
status: "fail",
|
||
message: "Bir hata oluştu"
|
||
});
|
||
}
|
||
|
||
await loginUser(request,userid);
|
||
|
||
return response.status(200).json({
|
||
status: "success",
|
||
message: "Kayıt işlemi başarılı, hesabınıza giriş yapabilirsiniz"
|
||
});
|
||
}
|
||
/**
|
||
* @param {import("express").Request} request
|
||
* @param {import("express").Response} response
|
||
*/
|
||
async function Login(request, response)
|
||
{
|
||
const error = loginValidation(request.body);
|
||
|
||
if(error)
|
||
{
|
||
return response.status(400).json({
|
||
status: "fail",
|
||
message: error.message
|
||
});
|
||
}
|
||
|
||
let findedUser = await User.checkUser(
|
||
request.body.email,
|
||
request.body.password
|
||
);
|
||
|
||
if(findedUser == null)
|
||
{
|
||
return response.status(400).json({
|
||
status: "fail",
|
||
message: "E-posta adresiniz veya şifreniz hatalı. Lütfen bilgilerinizi kontrol edip tekrar deneyiniz."
|
||
});
|
||
}
|
||
|
||
await loginUser(request,findedUser.id);
|
||
|
||
return response.status(200).json({
|
||
status: "success",
|
||
message: "Kayıt işlemi başarılı, hesabınıza giriş yapabilirsiniz"
|
||
});
|
||
}
|
||
|
||
function registerValidation(body)
|
||
{
|
||
const schema = Joi.object({
|
||
name: Joi.string()
|
||
.min(2).max(30).required()
|
||
.error(new Error('Ad zorunludur ve 2 ile 30 karakter arasında olmalıdır.')),
|
||
surname: Joi.string()
|
||
.min(2).max(30).required()
|
||
.error(new Error('Soyad zorunludur ve 2 ile 30 karakter arasında olmalıdır.')),
|
||
email: Joi.string()
|
||
.email()
|
||
.required()
|
||
.error(new Error('Geçerli bir e-posta adresi giriniz.')),
|
||
password: Joi.string()
|
||
.min(6).max(20).required()
|
||
.error(new Error('Şifre zorunludur ve en az 6 karakter olmalıdır.')),
|
||
passwordverif: Joi.any()
|
||
.equal(Joi.ref("password")).required()
|
||
.error(new Error('Şifreler eşleşmiyor'))
|
||
});
|
||
|
||
const {error} = schema.validate(body);
|
||
return error;
|
||
}
|
||
|
||
function loginValidation(body)
|
||
{
|
||
const schema = Joi.object({
|
||
email: Joi.string()
|
||
.email()
|
||
.required()
|
||
.error(new Error('Geçerli bir e-posta adresi giriniz.')),
|
||
password: Joi.string()
|
||
.min(6).max(20).required()
|
||
.error(new Error('Şifre zorunludur ve en az 6 karakter olmalıdır.'))
|
||
});
|
||
const {error} = schema.validate(body);
|
||
return error;
|
||
}
|
||
/**
|
||
* @param {import("express").Request} request
|
||
*/
|
||
async function loginUser(request,userId)
|
||
{
|
||
request.session.authendicated = true;
|
||
request.session.user_id = userId;
|
||
request.session.user = await User.getUser(userId);
|
||
request.session.save();
|
||
}
|
||
|
||
/**
|
||
* @param {import("express").Request} request
|
||
* @param {import("express").Response} response
|
||
*/
|
||
function MiddlewareAuth(request, response, next)
|
||
{
|
||
if(typeof request.session.authendicated == "boolean" && request.session.authendicated == true)
|
||
{
|
||
next()
|
||
}else{
|
||
response.redirect(307,"/login");
|
||
}
|
||
}
|
||
|
||
exports.MiddlewareAuth = MiddlewareAuth;
|
||
|
||
/**
|
||
* @param {import("express").Request} request
|
||
* @param {import("express").Response} response
|
||
*/
|
||
function Logout(request, response)
|
||
{
|
||
request.session.authendicated = false;
|
||
request.session.user_id = null;
|
||
request.session.user = null;
|
||
request.session.save();
|
||
response.redirect(307,"/login");
|
||
} |