Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xinz/ueberauth_openwechat
https://github.com/xinz/ueberauth_openwechat
Last synced: 15 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/xinz/ueberauth_openwechat
- Owner: xinz
- Created: 2021-06-01T09:09:17.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-06-01T09:10:22.000Z (over 3 years ago)
- Last Synced: 2024-12-21T14:07:49.931Z (17 days ago)
- Language: Elixir
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# UeberauthOpenWeChat
> WeChat OAuth2 strategy for Überauth.
## Installation
1. Setup your application at [WeChat Open Platform](https://open.weixin.qq.com/).
2. Add `:ueberauth_openwechat` to your list of dependencies in `mix.exs`.
3. Add WeChat to your Überauth configuration:
```elixir
config :ueberauth, Ueberauth,
providers: [
wechat: {Ueberauth.Strategy.WeChat, []}
]
```
4. Update your provider configuration:Use that if you want to read app ID/secret from the environment
variables in the compile time:```elixir
config :ueberauth, Ueberauth.Strategy.WeChat.OAuth,
appid: System.get_env("WECHAT_APP_ID"),
appsecret: System.get_env("WECHAT_APP_SECRET")
```Use that if you want to read app ID/secret from the environment
variables in the run time:```elixir
config :ueberauth, Ueberauth.Strategy.WeChat.OAuth,
appid: {System, :get_env, ["WECHAT_APP_ID"]},
appsecret: {System, :get_env, ["WECHAT_APP_SECRET"]}
```
Note: this configuration is optional if do not fetch user information in this service,
there provide a optional way to pass the `authorization code` to the third application,
and then fetch user information with the above mentioned `appid` and `appsecret` configuration.5. Include the Überauth plug in your controller:
```elixir
defmodule MyApp.AuthController do
use MyApp.Web, :controller
plug Ueberauth
...
end
```6. Create the request and callback routes if you haven't already:
```elixir
scope "/auth", MyApp do
pipe_through :browserget "/:provider", AuthController, :request
get "/:provider/callback", AuthController, :callback
end
```Note:
* If the defined service will pass the authorization code to the third application, the `/auth/:provider/callback`
url is useless.
* If the defined service will *ONLY* use the authorization code to fetch user information, the `/auth/:provider` url
is useless.7. Your controller needs to implement callbacks to deal with `Ueberauth.Auth` and `Ueberauth.Failure` responses,
for example:```elixir
defmodule MyApp.AuthController do
use UehelloWeb, :controllerplug Ueberauth
alias Ueberauth.Strategy.Helpers
def request(conn, _params) do
render(conn, "request.html", callback_url: Helpers.callback_url(conn))
enddef callback(%{assigns: %{ueberauth_failure: fails}} = conn, _params) do
conn
|> put_flash(:error, "Failed to authenticate.")
|> redirect(to: "/")
enddef callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
IO.inspect(auth)
...
end
end
```## Calling
Depending on the configured url you can initiate the request through:
```text
/auth/wechat
```Or with options:
```text
/auth/wechat?appid=YOUR_APPID
```Custom the lang parameter to call the [snsapi_userinfo](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html) API
```text
/auth/wechat?appid=YOUR_APPID&lang=zh_CN
```If you want to pass the authorization code to the third application (e.g "https://yourthirdapp.com/auth/wechat/callback") to finish OAuth2:
```text
/auth/wechat?appid=YOUR_APPID&lang=zh_CN&redirect_uri=https%3A%2F%2Fyourthirdapp.com%2Fauth%2Fwechat%2Fcallback
```In this case, after a user authorized, we will see the authorization code parameter into like this:
```text
https://yourthirdapp.com/auth/wechat/callback?code=CODE
```You can fetch the authorized user information in the third application by the `code` and the configured `appid` and `appsecret`.
## License
MIT