首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在React.js中使用hello.js

在React.js中使用hello.js
EN

Stack Overflow用户
提问于 2015-07-16 23:29:19
回答 1查看 1.6K关注 0票数 5

我想了解如何使Hello.js与React.js一起工作,尤其是自定义事件处理程序hello.on

由于我是React.js的新手,我不知道如何将非React事件绑定到应用程序流中。

我尝试将事件处理程序放在componentDidMount处理程序中

代码语言:javascript
复制
    handleClick(){
    hello('twitter').login();
}

componentDidMount(){
    hello.on('auth.login', function(auth) {

    // Call user information, for the given network
        hello(auth.network).api('/me').then(function(r) {
            console.log(r);
        });
    });
    hello.init({
    'twitter' : 'J1jqqO50tcLtLx8Js0VDitjZW'
    },
    {
          redirect_uri:'/',
          oauth_proxy: 'https://auth-server.herokuapp.com/proxy'
    });

}

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-09-14 20:09:38

三年后:

您需要一个用于身份验证的类,例如:

代码语言:javascript
复制
import * as React from "react";
import * as hello from "hellojs";
import { Event } from "../interfaces/Event";

export class Authentication extends React.Component<{}, { sendEvent: boolean }> {
  constructor(public props, public context) {
    super(props, context);
    this.state = {
      sendEvent: true
    };
  }
  public login(network) {
    hello.init({
      aad: {
        name: "Azure Active Directory",

        oauth: {
          version: 2,
          auth: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
          grant: "https://login.microsoftonline.com/common/oauth2/v2.0/token"
        },

        // Authorization scopes
        scope: {
          // you can add as many scopes to the mapping as you want here
          profile: "user.read",
          offline_access: ""
        },

        scope_delim: " ",

        login: p => {
          if (p.qs.response_type === "code") {
            // Let's set this to an offline access to return a refresh_token
            p.qs.access_type = "offline_access";
          }
        },

        base: "https://www.graph.microsoft.com/v1.0/",

        get: {
          me: "me"
        },

        xhr: p => {
          if (p.method === "post" || p.method === "put") {
            JSON.parse(p);
          } else if (p.method === "patch") {
            hello.utils.extend(p.query, p.data);
            p.data = null;
          }

          return true;
        },

        // Don't even try submitting via form.
        // This means no POST operations in <=IE9
        form: false
      }
    });
    hello.init(
      {
        aad: "ClientID"
      },
      {
        redirect_uri: "YOUR REDIRECT_URI",
        //redirect_uri: 'https://localhost:4321/temp/workbench.html',
        scope: "user.read"
      }
    );
    // By defining response type to code, the OAuth flow that will return a refresh token to be used to refresh the access token
    // However this will require the oauth_proxy server
    hello(network)
      .login({ display: "none" })
      .then(
        authInfo => {
          console.log(authInfo);
          localStorage.setItem("logged", authInfo.authResponse.access_token);
        },
        e => {
          console.error("Signin error: " + e.error.message);
        }
      );
  }
  //when the component is mounted you check the localstorage
  //logged ==> undefined you call login and save a token in localstorage
  //logged ==> with a token -> setEvent call a function that use graph api
  public componentDidMount() {
    let logged = localStorage["logged"];
    if (logged === undefined) this.login("aad");
    else {
      if (this.state.sendEvent) {
        this.props.setEvent(null);
        this.props.setEvent(Event.GET_ALL_USERS);
      }
    }
  }

  public render() {
    return null;
  }
}

文件名为auth.tsx,您可以在主react类中调用该类:

代码语言:javascript
复制
export class mainClass extends React.Component{
  ......
  ......
  private getEvent = (event) => {
    this.setState({ event: event });
    //HERE YOU recive the event when auth is ready
  }
  public render(){
    <Authentication setEvent={this.getEvent} />
  }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31458502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档