GithubHelp home page GithubHelp logo

overtrue / laravel-stateless-session Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 1.0 13 KB

A lightweight middleware to make api routing session capable.

License: MIT License

PHP 100.00%
laravel laravel-api session laravel-stateless

laravel-stateless-session's Introduction

Laravel stateless session

A lightweight middleware to make api routing session capable.

Sponsor me

Installing

$ composer require overtrue/laravel-stateless-session -vvv

Usage

Add the middleware (\Overtrue\LaravelStatelessSession\Http\Middleware\SetSessionFromHeaders) to api route group.

protected $middlewareGroups = [
        //...
        'api' => [
            \Overtrue\LaravelStatelessSession\Http\Middleware\SetSessionFromHeaders::class,
            \Illuminate\Session\Middleware\StartSession::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

Response header

This middleware will set session id to response headers with name 'x-session', if you want to update the header name, you can add header to config/session.php:

config/session.php

    'header' => 'x-session',

Request header

The api request must set the last session id to headers. Using axios as an example, we need to listen to the api response, retrieve the session ID from response headers and store it in local storage, then retrieve it and add it to the request header:

axios.interceptors.request.use(function (config) {
    config.headers['x-session'] = 'session id from your localstorage';
    return config;
  }, function (error) {
    return Promise.reject(error);
  });

// Store the response session id
axios.interceptors.response.use(function (response) {
    if (response.headers['x-session']) {
        // store session id to localstorage
    }
    return response;
  }, function (error) {
    return Promise.reject(error);
  });

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT

laravel-stateless-session's People

Contributors

overtrue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

wjfz

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.