Skip to content

Commit 2787390

Browse files
committed
Create OTP without model
1 parent 86f62d0 commit 2787390

File tree

5 files changed

+168
-26
lines changed

5 files changed

+168
-26
lines changed

README.md

+75-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
- [x] Generate OTP
1919
- [x] Verify OTP
20-
- [x] OTP Lists
20+
- [x] Methods
21+
- [ ] OTP Lists
2122
- [ ] Test Cases
2223

2324
## Features
@@ -35,6 +36,12 @@ You can install this package via composer using:
3536
composer require signaturetech/laravel-otp
3637
```
3738

39+
Next run the command below to setup api-response.config file, you can set your configuration.
40+
41+
```
42+
php artisan vendor:publish --tag=otp-config
43+
```
44+
3845
Now add the `use SignatureTech\LaravelOtp\Traits\Otpable` trait to your model.
3946

4047
```
@@ -58,7 +65,7 @@ use App\Models\User;
5865
$user = User::first();
5966
```
6067

61-
2. Create Otp Insatance
68+
2. Create Otp Instance
6269

6370
```
6471
use SignatureTech\LaravelOtp\Otp;
@@ -81,7 +88,7 @@ $otp = $user->otp;
8188

8289
### Verify OTP
8390

84-
You can verifu otp by useing below code:
91+
You can verify otp by using below code:
8592

8693
1. Get the use details
8794

@@ -91,7 +98,7 @@ use App\Models\User;
9198
$user = User::first();
9299
```
93100

94-
2. Get Otp Insatance
101+
2. Get Otp Instance
95102

96103
```
97104
use SignatureTech\LaravelOtp\Otp;
@@ -112,6 +119,70 @@ try {
112119
}
113120
```
114121

122+
### Create OTP without model
123+
124+
You can also create otp without model using the following:
125+
126+
1. Create OTP
127+
128+
```
129+
use SignatureTech\LaravelOtp\Otp;
130+
131+
132+
$otp = Otp::for($user->email)->create();
133+
```
134+
135+
**Note:** You can use email/mobile/phone number to generate otp Just pass the detail using `for` method.
136+
137+
**Note:** You can use more method to setting otp all methods described in `methods` section.
138+
139+
2. Verify Otp
140+
141+
```
142+
try {
143+
$otp->verifyOtp($request->get('otp'));
144+
} catch (OtpInvalidException $e) {
145+
return $e->getMessage;
146+
} catch (OtpExpiredException $e) {
147+
return $e->getMessage;
148+
}
149+
```
150+
151+
## Methods
152+
153+
1. Set length of otp
154+
155+
```
156+
use SignatureTech\LaravelOtp\Otp;
157+
158+
// Set Length of OTP
159+
$otp = Otp::for($user->email)->setLength(4)->generate();
160+
```
161+
162+
**Note:** Default length is 6 digit and you can change the default digit to add the `OTP_LENGTH=4` in `.env` or `config/otp.php` file
163+
164+
2. Set Format (Available Format: alpha | alphanumeric | numeric)
165+
166+
```
167+
use SignatureTech\LaravelOtp\Otp;
168+
169+
// Set Format (Available Format: alpha | alphanumeric | numeric)
170+
$otp = Otp::for($user->email)->setFormat('numeric')->generate();
171+
```
172+
173+
**Note:** Default format is numeric and you can change the default format to add the `OTP_FORMAT=4` in `.env` or `config/otp.php` file
174+
175+
2. Set Expiry (In minutes)
176+
177+
```
178+
use SignatureTech\LaravelOtp\Otp;
179+
180+
// Set Format (Available Format: alpha | alphanumeric | numeric)
181+
$otp = Otp::for($user->email)->setExpiry(20)->generate();
182+
```
183+
184+
**Note:** Default expiry is 10 minutes and you can change this to add the `OTP_EXPIRY=20` in `.env` or `config/otp.php` file
185+
115186
## License
116187

117188
- Written and copyrighted ©2022 by Prem Chand Saini ([[email protected]](mailto:[email protected]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table(config('otp.table', 'otps'), function (Blueprint $table) {
15+
$table->string('event')->after('otp')->nullable();
16+
$table->bigInteger('model_id')->nullable()->change();
17+
$table->string('model_type')->nullable()->change();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*/
24+
public function down(): void
25+
{
26+
Schema::table(config('otp.table', 'otps'), function (Blueprint $table) {
27+
$table->dropColumn('event');
28+
$table->bigInteger('model_id')->change();
29+
$table->string('model_type')->change();
30+
});
31+
}
32+
};

src/Models/Otp.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace SignatureTech\LaravelOtp\Models;
44

5-
use Illuminate\Database\Eloquent\Factories\HasFactory;
65
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use SignatureTech\LaravelOtp\Exceptions\OtpExpiredException;
8+
use SignatureTech\LaravelOtp\Exceptions\OtpInvalidException;
79

810
class Otp extends Model
911
{
@@ -18,7 +20,8 @@ class Otp extends Model
1820
'receiver',
1921
'otp',
2022
'expired_at',
21-
'used_at'
23+
'used_at',
24+
'event'
2225
];
2326

2427
/**
@@ -28,4 +31,20 @@ public function getOtp()
2831
{
2932
return $this->otp;
3033
}
34+
35+
public function verifyOtp($otp)
36+
{
37+
if (now()->gt($this->expired_at)) {
38+
throw new OtpExpiredException(__('Otp Expired'));
39+
}
40+
41+
if ($this->otp != $otp) {
42+
throw new OtpInvalidException(__('Invalid Otp'));
43+
}
44+
45+
$this->used_at = now();
46+
$this->save();
47+
48+
return true;
49+
}
3150
}

src/Otp.php

+39-8
Original file line numberDiff line numberDiff line change
@@ -106,36 +106,67 @@ public function setFormat($format): self
106106
}
107107

108108
/**
109-
* @return OtpModel
109+
*
110+
* @return OtpModel|null
110111
*/
111-
public function generate()
112+
public function checkOtp(): OtpModel|null
112113
{
113-
// check exiting otp
114-
$otp = OtpModel::query()
114+
return OtpModel::query()
115115
->where('receiver', $this->receiver)
116116
->where('expired_at', '>=', now())
117117
->whereNull('used_at')
118118
->first();
119+
}
120+
121+
/**
122+
* @param string|null $event
123+
* @return OtpModel
124+
*/
125+
public function generate(string|null $event = null): OtpModel
126+
{
127+
// check exiting otp
128+
$otp = $this->checkOtp();
119129

120130
if ($otp) {
121131
return $otp;
122132
}
123133

124-
$this->generateOtp();
125-
126134
$model = new OtpModel([
127135
'receiver' => $this->receiver,
136+
'event' => $event,
128137
'otp' => app()->environment() == 'local' ? config('otp.default_otp') : $this->generateOtp(),
129138
'expired_at' => now()->addMinutes($this->expiry)
130139
]);
131140

132141
return $model;
133142
}
134143

144+
/**
145+
* @param string|null $event
146+
* @return OtpModel
147+
*/
148+
public function create(string|null $event = null): OtpModel
149+
{
150+
$otp = $this->checkOtp();
151+
152+
if ($otp) {
153+
return $otp;
154+
}
155+
156+
$otp = OtpModel::create([
157+
'receiver' => $this->receiver,
158+
'event' => $event,
159+
'otp' => app()->environment() == 'local' ? config('otp.default_otp') : $this->generateOtp(),
160+
'expired_at' => now()->addMinutes($this->expiry)
161+
]);
162+
163+
return $otp;
164+
}
165+
135166
/**
136167
* @return mixed
137168
*/
138-
protected function generateOtp()
169+
protected function generateOtp(): mixed
139170
{
140171
switch ($this->format) {
141172
case "alpha":
@@ -155,7 +186,7 @@ protected function generateOtp()
155186
/**
156187
* @return OtpModel
157188
*/
158-
public function getOtp()
189+
public function getOtp(): OtpModel
159190
{
160191
$otp = OtpModel::query()
161192
->where('receiver', $this->receiver)

src/Traits/Otpable.php

+1-12
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ public function createOtp($otp)
3737
*/
3838
public function verifyOtp($otpModel, $otp)
3939
{
40-
if (now()->gt($otpModel->expired_at)) {
41-
throw new OtpExpiredException(__('Otp Expired'));
42-
}
43-
44-
if ($otpModel->otp != $otp) {
45-
throw new OtpInvalidException(__('Invalid Otp'));
46-
}
47-
48-
$otpModel->used_at = now();
49-
$otpModel->save();
50-
51-
return true;
40+
return $otpModel->verifyOtp($otp);
5241
}
5342
}

0 commit comments

Comments
 (0)