Download the PHP package shetabit/token-builder without Composer
On this page you can find all versions of the php package shetabit/token-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download shetabit/token-builder
More information about shetabit/token-builder
Files in shetabit/token-builder
Package token-builder
Short Description Laravel Token Builder
License MIT
Homepage https://github.com/shetabit/token-builder
Informations about the package token-builder
Laravel Token Builder
This package can generate random tokens which can expires.
Generated token can be used for creating one-time links, or authentication with sms pin and etc.
we have 2 things that can expire genereted tokens:
- time limit : for example a token can be expired after 2022/05/12
- usage limit : for example a token can be expired after using it more than 3 times.
Donate me if you like this package :sunglasses: :bowtie:
List of contents
- Available drivers
- Install
- Configure
- How to use
- Generating tokens
- Adding expiration date
- Adding usage limit
- Validation
- Add relations
- Attach custom data
- Retrieve tokens
- TokenBuilder reference
- setUniqueId
- getUniqueId
- setData
- getData
- setExpireDate
- getExpireDate
- setUsageLimit
- getUsageLimit
- setType
- getType
- setRelatedItem
- getRelatedItem
- build
- findToken
- findValidToken
- Token (Eloquent) reference
- use
- hasUsed
- hasMaxUsageLimit
- hasExpired
- markAsExpired
- isValid
- tokenable
- Change log
- Contributing
- Security
- Credits
- License
Install
Via Composer
Configure
If you are using Laravel 5.5
or higher then you don't need to add the provider and alias.
In your config/app.php
file add these two lines.
then, run the below commands to publish migrations and create tables
How to use
you can use TokenBuilder
facade or Builder
class to build tokens.
Generating tokens
In your code, use facade like the below :
you can also use Builder
:
each tokenObject has a unique value that can be used to recognize it from others and we call it token
.
you can access to tokenObject unique token using token
, see below example:
you can retrieve token and send it to users via email or sms or using it in URLs. it can be used to sms verifications or one-time login pins , etc.
Adding expiration date
you can build a token with auto expiration date. this kind of token will be expired after the specified date.
Adding usage limit
you can add usage limit into your tokens. a token will be invalid after the usage has exceeded the limit.
Validation
you can validate a token using isValid
method.
a token is valid if has not exceeded the specified limit and has not expired yet.
you can use a token by running use
method.
you can expire a token by running markAsExpired
method. (this method will update expiration date to current date).
Add relations
you can add a relation to tokens. this can be done in 2 different ways:
Attach custom data
you can attach custom data into your token. this data will be stored in json format.
Retrieve tokens
you can retrieve tokenObject
using token's unique string
TokenBuilder reference
This is a reference for TokenBuilder methods.
TokenBuilder
creates a tokenObject (instance of eloquent Token
model) if you run build method and searches into data base using findToken
and findValidToken
.
-
setUniqueId
-
you can build tokens with your custom algoritm and use it as unique id.
-
getUniqueId
-
returns your custom unique id (if not exists, returns
null
). -
setData
-
you can attach some data into tokens and retrieve them later.
-
getData
returns attached data.
-
setExpireDate
sets an expiration date.
-
getExpireDate
retrieves expiration date.
-
setUsageLimit
sets usage limit.
-
getUsageLimit
retrieves usage limit.
-
setType
sets token type. you can set type for your tokens. its like a scope for your tokens.
-
getType
retrieves token type.
-
setRelatedItem
add a relation to token.
-
getRelatedItem
retrieve current relation.
-
build
generate a token.
-
findToken
find token (being valid or not valid is not important) and return
null
if not exists. -
findValidToken
find token if it is valid and return
null
if not exists.
Token Reference
-
use
add 1 to usage counter. (usageCounter = usageCounter + 1)
-
hasUsed
determine is current token has used and returns a boolean result.
-
hasMaxUsageLimit
determine if current token max usage is limited and returns a boolean result.
-
hasExpired
determine if current token has expired or not.
-
markAsExpired
mark current token as expired.
notice: this method updates
expired_at
field to current time (now). -
isValid
determines if current token is valid (must not be expired and must not be exceeded max usage limit).
-
tokenable
returns the token relation if there is any relation, and returns null if no relation exists.
Change log
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Mahdi khanzadi
- All Contributors
License
The MIT License (MIT). Please see License File for more information.