From efdfc798a09f06b2dc805921c15e3ee4cb096cda Mon Sep 17 00:00:00 2001 From: Michael Rode Date: Mon, 2 Sep 2019 23:01:39 -0500 Subject: [PATCH] Return list of users to admin page --- .prettierrc | 6 +++ client/src/actions/index.js | 2 +- client/src/components/Admin.js | 65 +++++++++++++------------- client/src/reducers/authReducer.js | 14 +++--- server/controllers/admin.controller.js | 11 +++++ server/index.js | 2 + server/routes/admin.route.js | 9 ++++ 7 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 .prettierrc create mode 100644 server/controllers/admin.controller.js create mode 100644 server/routes/admin.route.js diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..92dad6a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "semi": true, + "trailingComma": "all", + "singleQuote": true, + "printWidth": 70, +} \ No newline at end of file diff --git a/client/src/actions/index.js b/client/src/actions/index.js index 0ae0311..66fc310 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -22,7 +22,7 @@ export const fetchUser = () => async dispatch => { }; export const fetchUsers = () => async dispatch => { - const res = await axios.get('/api/admin/current_user'); + const res = await axios.get('/api/admin/users'); dispatch({ type: types.FETCH_USERS, payload: res.data }); }; diff --git a/client/src/components/Admin.js b/client/src/components/Admin.js index 6250cd7..c206213 100644 --- a/client/src/components/Admin.js +++ b/client/src/components/Admin.js @@ -6,49 +6,50 @@ import * as actions from './../actions'; import Typography from '@material-ui/core/Typography'; class Admin extends Component { - async componentDidMount() { + async componentDidMount() { await this.props.fetchUsers(); console.log('mike--', this.props.auth) } + + renderUsers() { + if (this.props.auth.users) { + const usersList = this.props.auth.users.map(user => { + return ( + + {user.email} + + ) + }) + return
{usersList}
+ } + } render() { if (!this.props) { return; } - if (!this.props.auth.admin) { - console.log('where are my props', this.props) + + if (!this.props.auth.user || !this.props.auth.user.admin) { + console.log('where are my props', this.props.auth) return

Checking for admin rights..

} - console.log(this.props.auth); - return ( -
- + console.log(this.props.auth); + return ( +
+ -
- - Select a user to login as   - - plex.com/link. - - -
-
- - A list of users - -
+
+ {this.renderUsers()}
- ); + +
+ ); } } diff --git a/client/src/reducers/authReducer.js b/client/src/reducers/authReducer.js index 5000ad8..aed672d 100644 --- a/client/src/reducers/authReducer.js +++ b/client/src/reducers/authReducer.js @@ -1,4 +1,4 @@ -import {types} from '../actions/index'; +import { types } from '../actions/index'; export const initialState = { loading: false, @@ -7,17 +7,17 @@ export const initialState = { users: '', }; -export default function(state = {}, action) { - console.log('action - payload', action.payload); +export default function (state = {}, action) { + console.log('action - payload', action.type); switch (action.type) { case types.FETCH_USER: - return action.payload || false; + return { ...state, user: action.payload }; case types.FETCH_USERS: - return action.payload || false; + return { ...state, users: action.payload }; case types.FETCH_PIN: - return {...state, plexPin: action.payload}; + return { ...state, plexPin: action.payload }; case types.CHECK_PLEX_PIN: - return {...state, plexToken: action.payload}; + return { ...state, plexToken: action.payload }; default: return state; } diff --git a/server/controllers/admin.controller.js b/server/controllers/admin.controller.js new file mode 100644 index 0000000..5fddce3 --- /dev/null +++ b/server/controllers/admin.controller.js @@ -0,0 +1,11 @@ +import { Router } from 'express'; +import models from '../db/models' +const router = Router(); + +router.get('/users', async (req, res) => { + const users = await models.User.findAll() + + res.send(users); +}); + +export default router; diff --git a/server/index.js b/server/index.js index cd4a647..3c0e50c 100644 --- a/server/index.js +++ b/server/index.js @@ -10,6 +10,7 @@ import tdaw from './routes/tdaw.route'; import movieDb from './routes/movieDb.route'; import sonarr from './routes/sonarr.route'; import auth from './routes/auth.route'; +import admin from './routes/admin.route'; import recommend from './routes/recommend.route'; require('./services/auth/passport'); @@ -44,6 +45,7 @@ export default () => { server.use('/api/recommend', recommend); server.use('/auth', auth); server.use('/api/auth', auth); + server.use('/api/admin', admin); if (process.env.NODE_ENV === 'production') { server.use(express.static('client/build')); diff --git a/server/routes/admin.route.js b/server/routes/admin.route.js new file mode 100644 index 0000000..4933458 --- /dev/null +++ b/server/routes/admin.route.js @@ -0,0 +1,9 @@ +import adminController from '../controllers/admin.controller'; + +const express = require('express'); + +const router = express.Router(); + +router.use(adminController); + +export default router;