Skip to content

Commit 1be0dac

Browse files
committed
seprate module based routes and fix the broken frontend api
1 parent 571e5c8 commit 1be0dac

File tree

10 files changed

+63
-74
lines changed

10 files changed

+63
-74
lines changed

resources/assets/js/Main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import React, { Component } from 'react'
33
import PropTypes from 'prop-types'
44
import { connect } from 'react-redux'
5-
import { fetchUser } from './store/services/user'
5+
6+
// import services actions
7+
import { fetchUser } from './store/services/auth'
68

79
// import components
810
import Navigation from './common/navigation'

resources/assets/js/store/action-types/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const AUTH_LOGIN = 'AUTH_LOGIN'
44
export const AUTH_LOGOUT = 'AUTH_LOGOUT'
55
export const AUTH_REFRESH_TOKEN = 'AUTH_REFRESH_TOKEN'
66
export const AUTH_RESET_PASSWORD = 'AUTH_RESET_PASSWORD'
7+
export const AUTH_USER = 'AUTH_USER'
78

89
// user action types
910
export const USER_UPDATE = 'USER_UPDATE'
@@ -21,6 +22,7 @@ export default {
2122
AUTH_LOGOUT,
2223
AUTH_REFRESH_TOKEN,
2324
AUTH_RESET_PASSWORD,
25+
AUTH_USER,
2426

2527
USER_UPDATE,
2628
USER_UNSET,

resources/assets/js/store/actions/auth.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
AUTH_LOGOUT,
1313
AUTH_REFRESH_TOKEN,
1414
AUTH_RESET_PASSWORD,
15+
AUTH_USER,
1516
} from '../action-types';
1617

1718

@@ -46,3 +47,10 @@ export function authResetPassword() {
4647
type: AUTH_RESET_PASSWORD,
4748
}
4849
}
50+
51+
export function authUser(payload) {
52+
return {
53+
type: AUTH_USER,
54+
payload
55+
}
56+
}

resources/assets/js/store/reducers/auth.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ import {
77
AUTH_RESET_PASSWORD,
88
} from '../action-types';
99

10-
const user = {
11-
id: null,
12-
name: null,
13-
email: null,
14-
createdAt: null,
15-
updatedAt: null,
16-
}
17-
1810
const initialState = {
1911
isAuthenticated: false,
2012
};
@@ -38,10 +30,10 @@ const reducer = (state = initialState, { type, payload = null }) => {
3830
function login(state, payload) {
3931
localStorage.setItem('access_token', payload);
4032
HTTP.defaults.headers.common['Authorization'] = `Bearer ${payload}`;
41-
42-
state = Object.assign({}, state, { isAuthenticated: true })
43-
44-
return state
33+
34+
return {
35+
...state, isAuthenticated: true,
36+
}
4537
}
4638

4739
function checkAuth(state) {
@@ -58,18 +50,16 @@ function checkAuth(state) {
5850

5951
function logout(state) {
6052
localStorage.removeItem('access_token')
61-
62-
state = Object.assign({}, state, {
63-
isAuthenticated: false,
64-
user,
65-
})
66-
67-
return state;
53+
54+
return {
55+
...state, isAuthenticated: false
56+
}
6857
}
6958

7059
function resetPassword(state) {
71-
state.resetPassword = true;
72-
return state;
60+
return {
61+
...state, resetPassword: true,
62+
}
7363
}
7464

7565
export const getAuth = state => state.auth.isAuthenticated;
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import User from '../../models/User'
2-
import { USER_UPDATE , USER_UNSET, AUTH_LOGOUT } from '../action-types'
2+
import { USER_UPDATE , USER_UNSET, AUTH_LOGOUT, AUTH_USER} from '../action-types'
33

44
const initialState = Object.assign({}, new User({}))
55

66
const reducer = (state = initialState, { type, payload = null }) => {
77
switch (type) {
8+
case AUTH_USER:
9+
return authUser(state, payload)
810
case USER_UPDATE:
911
return updateUser(state, payload);
1012
case AUTH_LOGOUT:
@@ -16,17 +18,21 @@ const reducer = (state = initialState, { type, payload = null }) => {
1618
}
1719

1820
function updateUser(state, payload) {
19-
state = Object.assign({}, state, payload)
20-
21-
return state
21+
return {
22+
...state, ...payload
23+
}
2224
}
2325

2426
function unsetUser(state) {
25-
const user = Object.assign({}, new User({}))
26-
27-
state = Object.assign({}, state, user)
28-
29-
return state
27+
return {
28+
...state, initialState
29+
}
30+
}
31+
32+
function authUser(state, user) {
33+
return {
34+
...state, ...user
35+
}
3036
}
3137

3238
export default reducer

resources/assets/js/store/services/auth.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ import Http from '../../utils/Http'
22
import { authActions } from '../actions'
33
import Transformer from '../../utils/Transformer'
44

5+
/**
6+
* fetch the current logged in user
7+
*
8+
* @returns {function(*)}
9+
*/
10+
export function fetchUser() {
11+
return dispatch => {
12+
return Http.get('auth/user')
13+
.then(res => {
14+
const data = Transformer.fetch(res.data)
15+
dispatch(authActions.authUser(data))
16+
})
17+
.catch(err => {
18+
console.log(err)
19+
})
20+
}
21+
}
22+
523
/**
624
* login user
725
*

resources/assets/js/store/services/user.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@ import Http from '../../utils/Http'
22
import Transformer from '../../utils/Transformer'
33
import { userActions } from '../actions'
44

5-
export function fetchUser() {
6-
return dispatch => {
7-
return Http.get('user')
8-
.then(res => {
9-
const data = Transformer.fetch(res.data)
10-
dispatch(userActions.userUpdate(data))
11-
})
12-
.catch(err => {
13-
console.log(err)
14-
})
15-
}
16-
}
17-
185
export function userUpdateRequest(params) {
196
return dispatch => (
207
new Promise((resolve, reject) => {
@@ -45,14 +32,4 @@ export function userUpdateRequest(params) {
4532
})
4633
})
4734
)
48-
/*return dispatch => {
49-
const data = Transformer.send(param)
50-
Http.patch(`/users/${data.id}`, data)
51-
.then((res) => {
52-
dispatch(userActions.userUpdate(Transformer.fetch(res.data)))
53-
})
54-
.catch((err) => {
55-
console.log(err)
56-
})
57-
}*/
5835
}

routes/api.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
// default name space for all routes is 'App\Http\Controllers\Api'
17-
1817
$api_version = config('api.api_version');
1918

2019
Route::group(["prefix" => "{$api_version}"], function() {
@@ -28,20 +27,3 @@
2827
Route::prefix('articles')
2928
->group(base_path('routes/api/articles.php'));
3029
});
31-
32-
/*Route::post('auth/register', 'Auth\RegisterController@register')->name('auth.register');
33-
Route::post('auth/login', 'Auth\LoginController@login')->name('auth.login');
34-
Route::get('articles/published', 'ArticleController@publishedArticles')->name('articles.published.index');
35-
Route::get('articles/published/{id}', 'ArticleController@publishedArticle')->name('articles.published.show');
36-
37-
Route::group(['middleware' => 'auth:api'], function() {
38-
Route::get('/user', function (Request $request) {
39-
return $request->user();
40-
});
41-
42-
Route::patch('/users/{id}', 'UserController@update')->name('users.update');
43-
44-
Route::resource('articles', 'ArticleController', ['except' => ['edit', 'create']]);
45-
46-
Route::delete('/auth/logout', 'Auth\LoginController@logout')->name('auth.logout');;
47-
});*/

routes/api/articles.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
Route::get('published/{id}', 'ArticleController@publishedArticle')->name('articles.published.show');
77

88
Route::group(['middleware' => 'auth:api'], function() {
9-
Route::apiResource('/', 'ArticleController');
9+
Route::post('/', 'ArticleController@store')->name('articles.store');
10+
Route::get('/', 'ArticleController@index')->name('articles.index');
11+
Route::get('/{id}', 'ArticleController@show')->name('articles.show');
12+
Route::match(['put', 'patch'], '/{id}', 'ArticleController@update')->name('articles.update');
13+
Route::delete('/{id}', 'ArticleController@delete')->name('articles.delete');
1014
});

routes/api/users.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
use Illuminate\Support\Facades\Route;
44

55
Route::group(['middleware' => 'auth:api'], function() {
6-
Route::patch('/{id}', 'UserController@update')->name('users.update');
6+
Route::match(['put', 'patch'], '/{id}', 'UserController@update')->name('users.update');
77
});

0 commit comments

Comments
 (0)