Giter Club home page Giter Club logo

redux-implicit-oauth2's Introduction

Build Status Coverage Status

redux-implicit-oauth2

OAuth 2.0 Implicit Grant Flow with Redux.

Example (with React)

The following example displays either a login or logout button depending on the state. Set the config object according to your OAuth 2.0 server parameters. The redirect callback page should be on the same site as the rest of your app.

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { login, logout } from 'redux-implicit-oauth2'

const config = {
  url: "https://example.com/authorize",
  client: "some_client_id",
  redirect: "https://example.com/callback.html",
  scope: "some_scope",
  width: 400, // Width (in pixels) of login popup window. Optional, default: 400
  height: 400 // Height (in pixels) of login popup window. Optional, default: 400
}

const Login = ({ isLoggedIn, login, logout }) => {
  if (isLoggedIn) {
    return <button type='button' onClick={logout}>Logout</button>
  } else {
    return <button type='button' onClick={login}>Login</button>
  }
}

Login.propTypes = {
  isLoggedIn: PropTypes.bool.isRequired,
  login: PropTypes.func.isRequired,
  logout: PropTypes.func.isRequired
}

const mapStateToProps = ({ auth }) => ({
  isLoggedIn: auth.isLoggedIn
})

const mapDispatchToProps = {
  login: () => login(config),
  logout
}

export default connect(mapStateToProps, mapDispatchToProps)(Login)

Don't forget to add the reducer and middleware to your Redux store:

import { createStore, combineReducers, applyMiddleware } from 'redux'
import { authMiddleware, authReducer as auth } from 'redux-implicit-oauth2'

const configureStore = (initialState) =>
  createStore(
    combineReducers({
      // other reducers
      auth
    }),
    initialState,
    applyMiddleware(
      // other middleware
      authMiddleware
    )
  )

export default configureStore

redux-implicit-oauth2's People

Contributors

bcongdon avatar danilobuerger avatar duergner avatar oayandosu avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

redux-implicit-oauth2's Issues

The login() action creator isn't setting the isLoggingIn store item

I can make a call to dispatch(login(config)) and cause the login window to pop-up. I can successfully get a token, and the store state is populated as expected. If I choose to close the pop-up without authenticating the store state is also as expected.
For some reason, though, I don't see a 'LOGIN_REQUEST' action taking place, nor does isLoggingIn get set. Any ideas what could be going wrong?

Not being redirected to callback URL

Hi,

I am trying to integrate this library with my React app (created using create-react-app). I have followed the example setup but once I have authenticated, the popup closes but I am not taken to the redirect url and I can't see the auth token. I've tried testing the implicit flow with my auth server and that seems to be working. What am I doing wrong?

Here are my app files:

App.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Login from './Login.js'
import store from './store';
import { Provider } from 'react-redux';

class App extends Component {
  render() {
    return (

      <div className="App">
        <Provider store={store}>
          <header className="App-header">
            <img src={logo} className="App-logo" alt="logo" />
            <h1 className="App-title">React OAuth Test</h1>
            <Login />
          </header>
        </Provider>
      </div>
    );
  }
}
export default App;

Login.js

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { login, logout } from 'redux-implicit-oauth2'
 
const config = {
  url: "http://localhost:8080/oauth/authorize",
  client: "clientid",
  redirect: "http://localhost:3000/callback.html",
  scope: "webclient",
  width: 400, // Width (in pixels) of login popup window. Optional, default: 400
  height: 400 // Height (in pixels) of login popup window. Optional, default: 400
}
 
const Login = ({ isLoggedIn, login, logout }) => {
  if (isLoggedIn) {
    return <button type='button' onClick={logout}>Logout</button>
  } else {
    return <button type='button' onClick={login}>Login</button>
  }
}
 
Login.propTypes = {
  isLoggedIn: PropTypes.bool.isRequired,
  login: PropTypes.func.isRequired,
  logout: PropTypes.func.isRequired
}
 
const mapStateToProps = ({ auth }) => ({
  isLoggedIn: auth.isLoggedIn
})
 
const mapDispatchToProps = {
  login: () => login(config),
  logout
}
 
export default connect(mapStateToProps, mapDispatchToProps)(Login)
store.js

store.js

import { createStore, combineReducers, applyMiddleware } from 'redux'
import { authMiddleware, authReducer as auth } from 'redux-implicit-oauth2'

export default createStore(
    combineReducers({
      // other reducers
      auth
    }),
    applyMiddleware(
      // other middleware
      authMiddleware
    )
  )

Setting for size of popup window

It would be nice if you could specify the size of the popup window.
It's currently locked to 400x400, but some Oauth authorization pages are too large to be displayed in this window size.

Make response type configurable

Hey I'm trying to use this with an api where the reponse_type sent needs to be set as code not token. Would be great if this was configurable.

Property in store to mark as "authentication in progress"

There's currently no way to tell by the state of the store if authentication is in progress.

You can infer by 'isLoggedIn' and 'error', but this leaves discrepancies. For example, if you fail an authentication and then fail again, there's no way to tell when the second failure has occurred.

The library fails to fetch state and access token correctly for Instagram implicit oauth

Based on Instagram's documentation, for the Client-Side (Implicit) Authentication they provide the response to the authentication request as below:
http://your-redirect-uri?state=STATE#access_token=ACCESS-TOKEN
As can be seen from the above, only the access token is provided as a fragment in the url. However, currently the library query-string in oauth2.js to fetch the value after the hash and searches for state and access_token in it. This process makes the library unable to fetch the state correctly and even upon successful authentication it always returns:
{type: "LOGIN_FAILURE", error: "Invalid state returned."} as the login function's return value. I can volunteer to create a pull request to fix this.

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.