Skip to content

edwardzhou/ueberauth_wechat_miniapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Überauth Wechat Miniapp

Wechat Miniapp OAuth2 strategy for Überauth.

Installation

  1. Add :ueberauth_wechat_miniapp to your list of dependencies in mix.exs:

    def deps do
      [{:ueberauth_wechat_miniapp, github: "edwardzhou/ueberauth_wechat_miniapp"}]
    end
  2. Add the strategy to your applications:

    def application do
      [applications: [:ueberauth_wechat_miniapp]]
    end
  3. Add Wechat to your Überauth configuration:

    config :ueberauth, Ueberauth,
      providers: [
        wechat_miniapp: {Ueberauth.Strategy.WechatMiniapp, []}
      ]
  4. Update your provider configuration:

    config :ueberauth, Ueberauth.Strategy.WechatMiniapp.OAuth,
      client_id: System.get_env("WECHAT_MINIAPP_APPID"),
      client_secret: System.get_env("WECHAT_MINIAPP_SECRET")
  5. Include the Überauth plug in your controller:

    defmodule MyApp.AuthController do
      use MyApp.Web, :controller
    
      pipeline :browser do
        plug Ueberauth
        ...
       end
    end
  6. Create the request and callback routes if you haven't already:

    scope "/auth", MyApp do
      pipe_through :browser
    
      get "/:provider/callback", AuthController, :callback
      post "/:provider/callback", AuthController, :callback
    end
  7. You controller needs to implement callbacks to deal with Ueberauth.Auth and Ueberauth.Failure responses.

For an example implementation see the Überauth Example application.

Calling

get or post
/auth/wechat_miniapp/callback?code=<js_code>&iv=<iv>&encrypted_data=<encrypted_data>&signature=<signature>&raw_data=<raw_data>

Wechat Miniapp oauth sample:

    // try login
    // 登录
    wx.login({
        success: res => {
        if (res.code) {
            const code = res.code;

            wx.getUserInfo({withCredentials: true,
            success: user_res => {
                var form_data = {
                code: code,
                raw_data: user_res.rawData,
                signature: user_res.signature,
                encrypted_data: user_res.encryptedData,
                iv: user_res.iv
                }

                wx.request({
                url: 'https://your.domain.com/auth/wechat_miniapp/callback',
                data: form_data,
                success: login_res => {
                    console.log("login response: " + JSON.stringify(res));
                },
                fail: res => {
                console.log("login failed!")
                }
                })
            }
            })
        }
        }
    })

About

Wechat Miniapp OAuth2 strategy for Überauth.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages