84 lines
3.0 KiB
PHP
Raw Normal View History

2024-05-29 12:29:53 +02:00
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Connect extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('User_model');
$this->load->library('session');
}
public function create()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('nom', 'Nom', 'required');
$this->form_validation->set_rules('prenom', 'Prénom', 'required');
$this->form_validation->set_rules('email', 'Adresse mail', 'valid_email|required');
$this->form_validation->set_rules('password', 'Password', 'min_length[5]|required');
$this->form_validation->set_rules('cpassword', 'Confirmation Password', 'required|matches[password]');
if ($this->form_validation->run() === FALSE) {
$this->load->view('layout/header');
$this->load->view('create');
$this->load->view('layout/footer');
} else {
$data = array(
'nom' => $this->input->post('nom'),
'prenom' => $this->input->post('prenom'),
'email' => $this->input->post('email'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT)
);
if ($this->User_model->create_user($data)) {
// Redirect to a success page or login page
redirect('connect/login');
} else {
// Handle error
$this->load->view('layout/header');
$this->load->view('create');
$this->load->view('layout/footer');
}
}
}
public function login()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('layout/header');
$this->load->view('login');
$this->load->view('layout/footer');
} else {
$email = $this->input->post('email');
$password = $this->input->post('password');
$user = $this->User_model->get_user_by_email($email);
if ($user && password_verify($password, $user['password'])) {
// Set session data and redirect to a protected page
$this->session->set_userdata('user_id', $user['id']);
redirect('albums');
} else {
// Handle login error
$data['error'] = 'Adresse email ou mot de passe incorrect';
$this->load->view('layout/header');
$this->load->view('login', $data); // Passer le message d'erreur à la vue
$this->load->view('layout/footer');
}
}
}
public function logout()
{
$this->session->unset_userdata('user_id');
redirect('connect/login');
}
}