Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit b207b71

Browse files
committed
✨ Added base factory
1 parent 98a34f7 commit b207b71

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

App/Controllers/Controller.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// Leaf Auth is a package which makes user authentication simple
55
use Leaf\Auth;
6+
use Leaf\Helpers\Password;
67

78
/**
89
* This is the base controller for your Leaf API Project.
@@ -16,16 +17,32 @@ public function __construct() {
1617
parent::__construct();
1718

1819
// In this version, request isn't initialised for you. You can use
19-
// requestData() to get request data or initialise it yourself
20+
// requestData() or request() to get request data or initialise it yourself
2021
$this->auth = new Auth;
22+
23+
// autoConnect uses the .env variables to quickly connect to db
2124
$this->auth->autoConnect();
2225

2326
// set default token expiry time
2427
$this->auth->tokenLifetime(60 * 60 * 24 * 365);
2528

2629
// You can configure auth to get additional customizations
30+
$this->auth->config("LOGIN_PARAMS_ERROR", "Username not registered!");
31+
32+
// Password encode is run when leaf wants to encode passwords on register
33+
// This exact method is used by default in Leaf, so you can delete it if
34+
// you want to.
2735
$this->auth->config("PASSWORD_ENCODE", function ($password) {
28-
return md5($password);
36+
return Password::hash($password);
2937
});
38+
39+
// this function is run to verify the password. It's done by default,
40+
// so you can remove this line and the above line if you wish to.
41+
$this->auth->config("PASSWORD_VERIFY", function ($password, $hashedPassword) {
42+
// Inside the password_verify method, you have access to the password and the hashed password
43+
return Password::verify($password, $hashedPassword);
44+
});
45+
46+
// You can refer to https://leafphp.netlify.app/#/leaf/v/2.4/core/auth for auth docs
3047
}
3148
}

App/Database/Factories/Factory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Database\Factories;
4+
5+
use Leaf\Factory as Base;
6+
use Illuminate\Support\Str;
7+
8+
/**
9+
* Base Factory Class
10+
* ----------------
11+
* You can define methods here that would be used
12+
* throughout your factory classes.
13+
*/
14+
class Factory extends Base
15+
{
16+
public $str;
17+
18+
public function __construct()
19+
{
20+
// Just for demo purposes, str is assigned here
21+
// so you can use it in all your factories.
22+
$this->str = Str::class;
23+
}
24+
}
25+

App/Database/Factories/UserFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace App\Database\Factories;
44

5-
use Leaf\Factory;
65
use App\Models\User;
7-
use Illuminate\Support\Str;
86

97
class UserFactory extends Factory
108
{
119
// If this model property isn't defined, Leaf will
1210
// try to generate the model name from the factory name
1311
public $model = User::class;
1412

13+
// You define your factory blueprint here
14+
// It should return an associative array
1515
public function definition()
1616
{
1717
return [
@@ -20,7 +20,8 @@ public function definition()
2020
'email' => $this->faker->unique()->safeEmail,
2121
'email_verified_at' => \Leaf\Date::now(),
2222
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
23-
'remember_token' => Str::random(10),
23+
// $this->str is defined in the base factory
24+
'remember_token' => $this->str::random(10),
2425
];
2526
}
2627
}

0 commit comments

Comments
 (0)