Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mongoose, {Schema} from 'mongoose';
- const UserSchema = new Schema({
- name: String,
- password: String,
- role: String
- });
- UserSchema.methods.canDoSomeBusinessLogic = function(){
- return this.name === 'Jeff';
- };
- UserSchema.methods.isAdmin = function(){
- return this.role === 'admin';
- };
- # JS Class definition - classes/user.js
- export default class User {
- constructor(data = {}){
- Object.assign(this,data);
- }
- canDoSomeBusinessLogic(){
- return this.name === 'Jeff';
- };
- isAdmin(){
- return this.role === 'admin';
- }
- }
- # Server - api/controllers/user.js
- import UserClass from
- User.findById(1,function(err,user){
- let user = new UserClass(user.toJSON();
- });
- # Client - reducers/User.js
- export default function authReducer(state = null, action) {
- switch (action.type) {
- case GET_USER:
- return new UserClass(action.response.data);
- }
- }
- # Client - containers/Page.jsx
- import {connect} from 'react-redux';
- @connect(state => ({user: state.user}))
- export default class Page extends React.Component {
- render(){
- if(this.props.user.isAdmin()){
- // Some admin
- }
- }
- }
- # helpers/user.js
- export function isAdmin(user){
- return user.role === 'admin';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement