Skip to content

Commit

Permalink
Sessão do Usuário
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasrmagalhaes committed Mar 27, 2022
1 parent be7126c commit 76ca606
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
);

if ($uri === '/' || $uri === '' || $uri === '/index.php') {
$uri = '/login.php';
$uri = '/day_records.php';
}

require_once(CONTROLLER_PATH . "/{$uri}");
2 changes: 2 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// Arquivos
require_once(realpath(dirname(__FILE__) . '/database.php'));
require_once(realpath(dirname(__FILE__) . '/loader.php'));
require_once(realpath(dirname(__FILE__) . '/session.php'));
require_once(realpath(MODEL_PATH . '/Model.php'));
require_once(realpath(MODEL_PATH . '/User.php'));
require_once(realpath(EXCEPTION_PATH . '/AppException.php'));
require_once(realpath(EXCEPTION_PATH . '/ValidationException.php'));
10 changes: 10 additions & 0 deletions src/config/session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

function requireValidSession() {
$user = $_SESSION['user'];

if (!isset($user)) {
header('Location: login.php');
exit();
}
}
4 changes: 4 additions & 0 deletions src/controllers/day_records.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<?php

session_start();

requireValidSession();

loadTemplateView('day_records');
5 changes: 5 additions & 0 deletions src/controllers/login.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

loadModel('Login');

session_start();

$exception = null;

if (count($_POST) > 0) {
Expand All @@ -9,6 +12,8 @@
try {
$user = $login->checkLogin();

$_SESSION['user'] = $user;

header("Location: day_records.php");
} catch (AppException $e) {
$exception = $e;
Expand Down
5 changes: 5 additions & 0 deletions src/controllers/logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

session_start();
session_destroy();
header('Location: login.php');
2 changes: 0 additions & 2 deletions src/models/Login.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

loadModel('User');

class Login extends Model {
public function validate() {
$errors = [];
Expand Down

0 comments on commit 76ca606

Please sign in to comment.