Skip to content

Commit d8e3ce3

Browse files
committed
✨ Added session methods
1 parent d0ff77e commit d8e3ce3

File tree

6 files changed

+70
-34
lines changed

6 files changed

+70
-34
lines changed

App/Routes/index.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
*/
2525
$app->setNamespace("\App\Controllers");
2626

27-
// $app is the instance of Leaf
27+
// New in Leaf v2.4.1.
28+
// You can assign a view directly
2829
$app->view("/", "index");
29-
30-
$app->get("/app", function () {
31-
json(app()->routes(), 200);
32-
});
33-

App/Views/assets/css/styles.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ strong {
2626
color: #038f03;
2727
}
2828

29-
a:hover {
30-
background-color: #eee;
31-
border-radius: 4px;
32-
}
33-
3429
.collection {
3530
display: flex;
3631
flex-direction: column;

App/Views/index.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</head>
1111

1212
<body>
13-
<div id="app">
13+
<div id="app" style="margin-top: 35px;">
1414
<section>
1515
<h1>{{ getenv('APP_NAME') ?? "Leaf MVC" }}</h1>
1616
<p>

Config/auth.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
|--------------------------------------------------------------------------
2121
|
2222
| Password encode is run when leaf wants to encode passwords on register
23-
| This exact method is used by default in Leaf, so you can delete it if
24-
| you want to.
23+
| This exact method is used by default in Leaf, so you can set it to null
24+
| if you want to.
25+
|
26+
| You can set your own implementation instead of Password::hash
2527
|
2628
*/
2729
"PASSWORD_ENCODE" => function ($password) {
@@ -33,12 +35,13 @@
3335
| Verify Password
3436
|--------------------------------------------------------------------------
3537
|
36-
| this function is run to verify the password. It's done by default,
37-
| so you can remove/set this and the above lines null if you wish to.
38+
| This function is run to verify the password. This implementation is done
39+
| by default, so you can set it to null, and it will still work fine.
40+
|
41+
| You can add your own implementation instead of Password::verify
3842
|
3943
*/
4044
"PASSWORD_VERIFY" => function ($password, $hashedPassword) {
41-
// Inside the password_verify method, you have access to the password and the hashed password
4245
return Password::verify($password, $hashedPassword);
4346
},
4447

@@ -47,7 +50,8 @@
4750
| Password Key
4851
|--------------------------------------------------------------------------
4952
|
50-
| the default password key
53+
| The default password key. Leaf will expect this key to hold passwords
54+
| in your database.
5155
|
5256
*/
5357
"PASSWORD_KEY" => "password",
@@ -57,7 +61,7 @@
5761
| Hide id
5862
|--------------------------------------------------------------------------
5963
|
60-
| Hide id field from user object?
64+
| Hide id field from user object returned in login, register and update
6165
|
6266
*/
6367
"HIDE_ID" => true,
@@ -67,21 +71,27 @@
6771
| Hide password
6872
|--------------------------------------------------------------------------
6973
|
70-
| Hide user password field
74+
| Hide password from user object returned in login, register and update
7175
|
7276
*/
7377
"HIDE_PASSWORD" => true,
7478

7579
/*
7680
|--------------------------------------------------------------------------
7781
| Login params error
82+
|--------------------------------------------------------------------------
83+
|
84+
| Error to show when the login params aren't found in db
7885
|
7986
*/
8087
"LOGIN_PARAMS_ERROR" => "Username not registered!",
8188

8289
/*
8390
|--------------------------------------------------------------------------
84-
| Login password error
91+
| Password error
92+
|--------------------------------------------------------------------------
93+
|
94+
| Error to show when the login password is wrong
8595
|
8696
*/
8797
"LOGIN_PASSWORD_ERROR" => "Password is incorrect!",
@@ -102,41 +112,40 @@
102112

103113
/*
104114
|--------------------------------------------------------------------------
105-
| Template Engine [EXPERIMENTAL]
115+
| Session on register
106116
|--------------------------------------------------------------------------
107117
|
108-
| Leaf MVC unlike other frameworks tries to give you as much control as
109-
| you need. As such, you can decide which view engine to use.
118+
| If true, a session will be created on a successful registration, else
119+
| you it'll be created on login rather.
110120
|
111121
*/
112-
// Create a session on registration?
113122
"SESSION_ON_REGISTER" => false,
114123

115124
/*
116125
|--------------------------------------------------------------------------
117126
| Login page route
118-
|
127+
|--------------------------------------------------------------------------
119128
*/
120129
"GUARD_LOGIN" => "/auth/login",
121130

122131
/*
123132
|--------------------------------------------------------------------------
124-
| Rehgister page route
125-
|
133+
| Register page route
134+
|--------------------------------------------------------------------------
126135
*/
127136
"GUARD_REGISTER" => "/auth/register",
128137

129138
/*
130139
|--------------------------------------------------------------------------
131140
| Logout route
132-
|
141+
|--------------------------------------------------------------------------
133142
*/
134143
"GUARD_LOGOUT" => "/auth/logout",
135144

136145
/*
137146
|--------------------------------------------------------------------------
138147
| Home page route
139-
|
148+
|--------------------------------------------------------------------------
140149
*/
141150
"GUARD_HOME" => "/home",
142151

Config/functions.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ function fs()
7070
}
7171
}
7272

73+
if (!function_exists('hasAuth')) {
74+
/**
75+
* Find out if there's an active sesion
76+
*/
77+
function hasAuth()
78+
{
79+
return !!sessionUser();
80+
}
81+
}
82+
7383
if (!function_exists('json')) {
7484
/**
7585
* json uses Leaf's now `json` method
@@ -182,6 +192,32 @@ function Route($methods, $pattern, $fn)
182192
}
183193
}
184194

195+
if (!function_exists('session')) {
196+
/**
197+
* Get a session variable or the session object
198+
*
199+
* @param string|null $key The variable to get
200+
*/
201+
function session($key = null)
202+
{
203+
if ($key) {
204+
return \Leaf\Http\Session::get($key);
205+
}
206+
207+
return (new \Leaf\Http\Session);
208+
}
209+
}
210+
211+
if (!function_exists('sessionUser')) {
212+
/**
213+
* Get the currently logged in user
214+
*/
215+
function sessionUser()
216+
{
217+
return session('AUTH_USER');
218+
}
219+
}
220+
185221
if (!function_exists('setHeader')) {
186222
/**
187223
* Set a response header

leaf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
| loading of any our classes "manually". Feels great to relax.
1212
|
1313
*/
14-
require __DIR__.'/vendor/autoload.php';
14+
require __DIR__ . '/vendor/autoload.php';
1515

1616
/*
1717
|--------------------------------------------------------------------------
@@ -21,7 +21,7 @@ require __DIR__.'/vendor/autoload.php';
2121
| Require all the files containing the Leaf Commands
2222
|
2323
*/
24-
require __DIR__.'/Config/bootstrap.php';
24+
require __DIR__ . '/Config/bootstrap.php';
2525

2626
/*
2727
|--------------------------------------------------------------------------
@@ -41,7 +41,7 @@ require __DIR__.'/Config/bootstrap.php';
4141
| Initialise aloe CLI
4242
|
4343
*/
44-
$console = new \Aloe\Console;
44+
$console = new \Aloe\Console("Leaf MVC", "v2.3");
4545

4646
/*
4747
|--------------------------------------------------------------------------
@@ -52,7 +52,7 @@ $console = new \Aloe\Console;
5252
|
5353
*/
5454
$console->register(\Aloe\Console::commands());
55-
$console->register(new \App\Console\ExampleCommand());
55+
$console->register(\App\Console\ExampleCommand::class);
5656

5757
/*
5858
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)