Skip to content

Instantly share code, notes, and snippets.

View mattiamanzati's full-sized avatar
💭
undefined is not a function

Mattia Manzati mattiamanzati

💭
undefined is not a function
View GitHub Profile
@mattiamanzati
mattiamanzati / handling auth in redux
Created August 25, 2015 16:27
Auth handling in redux.
Before reading:
This is a soft auth check. Auth check should always be done server side, if not, your rest api are not checking correctly for auth.
1. Auth is in the store.
You need to setup constants for AUTH_SET_TOKEN and AUTH_LOGOUT,
then create the action creators authSetToken(token) and authLogout() action creators.
Then setup a reducer that when AUTH_SET_TOKEN is dispatched, sets the internal state to {token: token},
and when logout is dispatched, clears it.
Now setup a utility function isLoggedIn(state) function, that only checks in state.auth.token exists, this is a soft check.
var React = require('react-native');
var {DeviceEventEmitter, View, StyleSheet} = React;
var styles = StyleSheet.create({
main: {
position: 'absolute',
top: 0,
left: 0,
right: 0
}
var React = require('react-native');
var EventEmitter = require('eventemitter3');
var MyTextInput = require('./TextInput');
var {View, Text, TextInput, TouchableOpacity, StyleSheet} = React;
var styles = StyleSheet.create({
wrapper: {
position: 'absolute',
top: 0,
left: 0,
// constants.js
module.exports = {
AUTH_SET_TOKEN: 'auth/SET_TOKEN',
AUTH_DISCARD_TOKEN: 'auth/DISCARD_TOKEN',
AUTH_SET_USER: 'auth/SET_USER'
};
var {
AUTH_SET_TOKEN,
AUTH_DISCARD_TOKEN,
AUTH_SET_USER
} = require('./constants');
function authSetToken(token){
return {
type: AUTH_SET_TOKEN,
token
var {
AUTH_SET_TOKEN,
AUTH_DISCARD_TOKEN,
AUTH_SET_USER
} = require('./constants');
function auth(state = {}, action){
switch(action.type){
// saves the token into the state
case AUTH_SET_TOKEN:
var {authDiscardToken} = require('./actions');
var request = require('superagent');
var API_ROOT = 'http://localhost/api';
/*
This below is the format the middleware accepts.
{
types: [ACT_LOGIN_PENDING, ACT_LOGIN_SUCCESS, ACT_LOGIN_ERROR],
url: '/auth/login',
// utility functions for the authLogin
function authLoginStart(){
return {
type: AUTH_LOGIN_START
};
}
function authLoginSuccess(user){
return {
type: AUTH_LOGIN_SUCCESS,
/*
import {withEffect} from 'redux-side-effects'
// inside your reducer you can do this
return withEffect(authUser, (effects) => effects.concat([
routerRedirectAction('/url/action'),
otherPossibleActionToDispatch('params')
]));
--------- beginning of /dev/log/system
I/Vold ( 333): [LGE][VOLD][NetlinkHandler.cpp][onEvent()] subsys:cpu, action:0
V/ActivityManager( 683): Moving to STOPPED: ActivityRecord{4299f7b0 u0 com.lge.launcher2/.Launcher t1} (stop complete)
D/NotificationService( 683): updateLightListLocked :r=null, action=2
D/NotificationService( 683): updateLightListLocked :r=NotificationRecord(0x43080ec0: pkg=android user=UserHandle{-1} id=17040542 tag=null score=0: Notification(pri=0 contentView=android/0x1090064 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[android.system.imeswitcher])), action=2