SoftwareBrothers / adminjs

AdminJS is an admin panel for apps written in node.js
https://adminjs.co
MIT License
8.21k stars 662 forks source link

Getting CurrentAdmin on custom jsx component #552

Closed hannyudayana closed 4 years ago

hannyudayana commented 4 years ago

Hi all, great work on the newest version of Adminbro.

I am using authenticatedRouter and I have successfully returned the admin object as follows:

authenticate: async (email, password) => {
  let loginResponse = null;
  try {
    const loginRequest = await axios.post(
      url
      { email: email, password: password },
      { withCredentials: true },
    );
    loginResponse = await loginRequest.data;
  } catch (err) {
    loginResponse = err.response.data;
  }
  if (loginResponse && loginResponse.success) {
    ADMIN.email = email;
    ADMIN.token = loginResponse.data.token;
    ADMIN.user_id = loginResponse.data.row.id;
    ADMIN.role_group = loginResponse.data.row.role_groups[0].name;
    return ADMIN
  }
  return null
},

The problem is, I would like to get the CurrentAdmin object on my custom component, but I always got undefined object. Below is my custom component code:

import React from 'react';

import { Box, Label, Button } from 'admin-bro/components';
import { CurrentAdmin } from 'admin-bro';

export default class myCustomComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            record: props.record,
            property: props.property,
        };
        console.log(CurrentAdmin);
    }
}

Versions: "admin-bro": "^2.9.0", "admin-bro-expressjs": "^2.1.1", "admin-bro-sequelizejs": "^0.5.1",

Any insight on this problem will be appreciated :) Cheers

wojtek-krysiak commented 4 years ago

hi { CurrentAdmin } is just a TypeScript type - it will be undefined in javascript.

In order to take the currentAdmin use this hook: https://adminbro.com/useCurrentAdmin.html