Giter Site home page Giter Site logo

lumen-jwt-api-boilerplate's Introduction

API with Lumen PHP Framework And JWT Auth

JWT Auth

Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Lumen attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as routing, database abstraction, queueing, and caching.

Official Documentation For Lumen

Documentation for the framework can be found on the Lumen website.

Official Documentation For JWT

Documentation for the JWT can be found on the JWT and Lumen.

Project Setup

This project requires latest composer version and git to run.

$ git clone https://[email protected]/kisorniru/siddique-api.local.git
$ cd siddique-api.local
$ composer update
  • copy and paste .env.dist file where it is AND rename it as .env file
  • open .env file and change inside DATABASE_URL
    • DataBase_userName

    • DataBase_password

    • DataBase_host

    • DataBase_name

    • MAIL_DRIVER=smtp

    • MAIL_HOST=smtp.gmail.com

    • MAIL_PORT=587

    • MAIL_USERNAME=[email protected]

    • MAIL_PASSWORD=yourgmailpass

    • MAIL_ENCRYPTION=tls

Project Installation

This project requires latest composer version and git to run.

  • run this command
$ composer create-project --prefer-dist laravel/lumen siddique-api.local
$ cd siddique-api.local
$ composer require tymon/jwt-auth:"^1.0@dev"
  • inside bootstrap/app.php

// Uncomment this line

$app->withFacades();
$app->withEloquent();
$app->register(App\Providers\AuthServiceProvider::class);

// Add this line

$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);

// Uncomment this line

$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);
  • run this command for JWT Secret
$ php artisan jwt:secret
  • For Email (Gmail SMTP) Issue

// Add this line into require section inside composer.json file

"illuminate/mail": "^5.7"

// run this command for composer update

$ composer update

// create config folder into root directory

// create mail.php file into config folder

// add bellow php code into mail.php file

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Mail Driver
    |--------------------------------------------------------------------------
    |
    | Laravel supports both SMTP and PHP's "mail" function as drivers for the
    | sending of e-mail. You may specify which one you're using throughout
    | your application here. By default, Laravel is setup for SMTP mail.
    |
    | Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "ses", "log"
    |
    */

    'driver' => env('MAIL_DRIVER', 'smtp'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Address
    |--------------------------------------------------------------------------
    |
    | Here you may provide the host address of the SMTP server used by your
    | applications. A default option is provided that is compatible with
    | the Mailgun mail service which will provide reliable deliveries.
    |
    */

    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Port
    |--------------------------------------------------------------------------
    |
    | This is the SMTP port used by your application to deliver e-mails to
    | users of the application. Like the host we have set this value to
    | stay compatible with the Mailgun e-mail application by default.
    |
    */

    'port' => env('MAIL_PORT', 587),

    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */

    'from' => ['address' => null, 'name' => null],

    /*
    |--------------------------------------------------------------------------
    | E-Mail Encryption Protocol
    |--------------------------------------------------------------------------
    |
    | Here you may specify the encryption protocol that should be used when
    | the application send e-mail messages. A sensible default using the
    | transport layer security protocol should provide great security.
    |
    */

    'encryption' => env('MAIL_ENCRYPTION', 'tls'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Username
    |--------------------------------------------------------------------------
    |
    | If your SMTP server requires a username for authentication, you should
    | set it here. This will get used to authenticate with your server on
    | connection. You may also set the "password" value below this one.
    |
    */

    'username' => env('MAIL_USERNAME'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Password
    |--------------------------------------------------------------------------
    |
    | Here you may set the password required by your SMTP server to send out
    | messages from your application. This will be given to the server on
    | connection so that the application will be able to send messages.
    |
    */

    'password' => env('MAIL_PASSWORD'),

    /*
    |--------------------------------------------------------------------------
    | Sendmail System Path
    |--------------------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -bs',

    /*
    |--------------------------------------------------------------------------
    | Mail "Pretend"
    |--------------------------------------------------------------------------
    |
    | When this option is enabled, e-mail will not actually be sent over the
    | web and will instead be written to your application's logs files so
    | you may inspect the message. This is great for local development.
    |
    */

    'pretend' => env('MAIL_PRETEND', false),

];

// Add this line into bootstrap/app.php file

$app->register(\Illuminate\Mail\MailServiceProvider::class);
$app->configure('mail');

// Add this line into .env file

- MAIL_DRIVER=smtp
- MAIL_HOST=smtp.gmail.com
- MAIL_PORT=587
- [email protected]
- MAIL_PASSWORD=yourgmailpass
- MAIL_ENCRYPTION=tls
  • Sending Email MailController.php
<?php

....

use Illuminate\Support\Facades\Mail;

class MailController extends Controller
{
    public function sendEmail(Request $request)
    {
        ....
        $this->sendEmailNotification($email, $name, $subject, $for_view_data_2);
        ....
    }

    ....
    ....

    private function sendEmailNotification($to_email, $to_name, $to_subject, $for_view_data_2)
    {
        $from_email = env('MAIL_USERNAME');
        $from_name = "Email Test";
        $to_email = $to_email;
        $to_name = $to_name;
        $to_subject = $to_subject;

        $sentMail = Mail::send('view_file_name', ['for_view_data_1' => $to_name, 'for_view_data_2' => $for_view_data_2 ], function($mail) use ($from_email, $from_name, $to_email, $to_name, $to_subject)
            {
                $mail->from($from_email, $from_name);
                $mail->to($to_email, $to_name);
                $mail->subject($to_subject);
            }
        );
    }
}

Any Problem

For any problem to run this project, please contact with me

email : [email protected]
phone : +88-01725-156-188

For More Reference

- https://medium.com/@edwardsteven/using-lumen-and-laravel-mail-with-mailgun-270415daed37
- https://medium.com/tech-tajawal/jwt-authentication-for-lumen-5-6-2376fd38d454
- https://medium.com/mesan-digital/tutorial-5-how-to-build-a-laravel-5-4-jwt-authentication-api-with-e-mail-verification-61d3f356f823
- https://stackoverflow.com/questions/40728207/lumen-5-3-send-email

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.