mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-10 17:12:48 +00:00
server/client - eu support for fetching characters
This commit is contained in:
@@ -19,6 +19,7 @@ interface State {
|
|||||||
selectedAvatarIndex: number;
|
selectedAvatarIndex: number;
|
||||||
insufficientScope?: boolean;
|
insufficientScope?: boolean;
|
||||||
noCharacters: boolean;
|
noCharacters: boolean;
|
||||||
|
region: 'us' | 'eu';
|
||||||
}
|
}
|
||||||
|
|
||||||
@inject('userStore')
|
@inject('userStore')
|
||||||
@@ -31,6 +32,7 @@ export class UserAccount extends React.Component<Props, State> {
|
|||||||
noCharacters: false,
|
noCharacters: false,
|
||||||
selectedCharIndex: 0,
|
selectedCharIndex: 0,
|
||||||
selectedAvatarIndex: 0,
|
selectedAvatarIndex: 0,
|
||||||
|
region: 'us', // default to US api
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,8 +50,9 @@ export class UserAccount extends React.Component<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getCharacters() {
|
async getCharacters() {
|
||||||
|
const { region } = this.state;
|
||||||
try {
|
try {
|
||||||
const res = await UserService.getCharacters() as any;
|
const res = await UserService.getCharacters({ region });
|
||||||
if (res.characters) {
|
if (res.characters) {
|
||||||
if (res.characters.length === 0) {
|
if (res.characters.length === 0) {
|
||||||
this.setState({ noCharacters: true });
|
this.setState({ noCharacters: true });
|
||||||
@@ -60,6 +63,7 @@ export class UserAccount extends React.Component<Props, State> {
|
|||||||
characters,
|
characters,
|
||||||
selectedRealm: res.characters[0].realm,
|
selectedRealm: res.characters[0].realm,
|
||||||
insufficientScope: false,
|
insufficientScope: false,
|
||||||
|
noCharacters: false,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.setState({ insufficientScope: true });
|
this.setState({ insufficientScope: true });
|
||||||
@@ -92,7 +96,9 @@ export class UserAccount extends React.Component<Props, State> {
|
|||||||
const { name, guild, realm } = this.selectedCharacter();
|
const { name, guild, realm } = this.selectedCharacter();
|
||||||
const selectedAvatar = this.selectedCharacter().avatarList![this.state.selectedAvatarIndex].title;
|
const selectedAvatar = this.selectedCharacter().avatarList![this.state.selectedAvatarIndex].title;
|
||||||
const charClass = CharacterService.getClass(this.selectedCharacter().class);
|
const charClass = CharacterService.getClass(this.selectedCharacter().class);
|
||||||
|
const { region } = this.state;
|
||||||
const data = {
|
const data = {
|
||||||
|
region,
|
||||||
character_name: name,
|
character_name: name,
|
||||||
character_class: charClass.name,
|
character_class: charClass.name,
|
||||||
character_guild: guild,
|
character_guild: guild,
|
||||||
@@ -165,21 +171,17 @@ export class UserAccount extends React.Component<Props, State> {
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
||||||
if (this.state.noCharacters) {
|
|
||||||
return <div>You have no WoW characters in your account.</div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
// user must be logged in to view this page
|
// user must be logged in to view this page
|
||||||
if (!this.props.userStore!.user) {
|
if (!this.props.userStore!.user) {
|
||||||
return <div></div>;
|
return <div></div>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { battletag, character_name, character_class, character_guild, character_realm, character_avatar } = this.props.userStore!.user!;
|
const { battletag, character_name, character_class, character_guild, character_realm, character_avatar } = this.props.userStore!.user!;
|
||||||
const { insufficientScope } = this.state;
|
const { insufficientScope, noCharacters } = this.state;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ScrollToTop>
|
<ScrollToTop>
|
||||||
<ContentContainer style={{ minHeight: '500px', paddingTop: '40px' }}>
|
<ContentContainer style={{ paddingTop: '40px' }}>
|
||||||
<div className="flex" style={{ marginBottom: '20px' }}>
|
<div className="flex" style={{ marginBottom: '20px' }}>
|
||||||
{character_avatar && <Portrait imageSrc={CharacterService.getAvatar(character_avatar!)}/>}
|
{character_avatar && <Portrait imageSrc={CharacterService.getAvatar(character_avatar!)}/>}
|
||||||
<div style={{ paddingLeft: '10px' }}>
|
<div style={{ paddingLeft: '10px' }}>
|
||||||
@@ -194,6 +196,14 @@ export class UserAccount extends React.Component<Props, State> {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{noCharacters &&
|
||||||
|
<div>
|
||||||
|
<p>Unable to fetch your characters.
|
||||||
|
<a onClick={() => this.setState({ region: 'eu' }, () => this.getCharacters())}>EU account?</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
{insufficientScope === true ? this.renderScopeError() : this.renderDropDowns()}
|
{insufficientScope === true ? this.renderScopeError() : this.renderDropDowns()}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ const login = async (username: string, password: string): Promise<any> => {
|
|||||||
userStore.setUser(res.data.data);
|
userStore.setUser(res.data.data);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getCharacters = async (): Promise<any> => {
|
const getCharacters = async (params: any): Promise<any> => {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get('/api/user/characters');
|
const res = await axios.get('/api/user/characters', { params });
|
||||||
const characters = res.data.data.characters;
|
const characters = res.data.data.characters;
|
||||||
if (!!characters) {
|
if (!!characters) {
|
||||||
res.data.data.characters = filterCharacters(characters);
|
res.data.data.characters = filterCharacters(characters);
|
||||||
|
|||||||
@@ -2,18 +2,20 @@ defmodule MyApp.BattleNet.User do
|
|||||||
|
|
||||||
@type battle_net_user :: %{"battle_net_id": integer, "battletag": String.t, "access_token": String.t}
|
@type battle_net_user :: %{"battle_net_id": integer, "battletag": String.t, "access_token": String.t}
|
||||||
|
|
||||||
def api_url, do: "https://us.api.battle.net"
|
def api_url(region), do: "https://#{region}.api.battle.net"
|
||||||
|
|
||||||
|
def cache_key(user_id, region), do: "usr_char:#{user_id}:#{region}"
|
||||||
|
|
||||||
# grab user information from battle net api - use token for auth
|
# grab user information from battle net api - use token for auth
|
||||||
@spec get_user(String.t | {atom, any}) :: {:ok, battle_net_user} | {:error, any}
|
@spec get_user(String.t | {atom, any}, String.t) :: {:ok, battle_net_user} | {:error, any}
|
||||||
def get_user(access_token) when is_binary(access_token) do
|
def get_user(access_token, region) when is_binary(access_token) do
|
||||||
access_token
|
access_token
|
||||||
|> resource_url("account/user")
|
|> resource_url("account/user", region)
|
||||||
|> HTTPoison.get
|
|> HTTPoison.get
|
||||||
|> parse_user_response(access_token)
|
|> parse_user_response(access_token)
|
||||||
end
|
end
|
||||||
def get_user({:ok, access_token}), do: get_user(access_token)
|
def get_user({:ok, access_token}, region), do: get_user(access_token, region)
|
||||||
def get_user({:error, error}), do: {:error, error}
|
def get_user({:error, error}, _), do: {:error, error}
|
||||||
|
|
||||||
defp parse_user_response({:error, error}, _), do: {:error, error}
|
defp parse_user_response({:error, error}, _), do: {:error, error}
|
||||||
defp parse_user_response({:ok, %HTTPoison.Response{body: body}}, access_token) do
|
defp parse_user_response({:ok, %HTTPoison.Response{body: body}}, access_token) do
|
||||||
@@ -28,36 +30,37 @@ defmodule MyApp.BattleNet.User do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# end point is cached for one minute per user
|
# end point is cached for ten minutes per user
|
||||||
@spec get_user_characters(integer, String.t) :: {:ok, map} | {:error, any}
|
@spec get_user_characters(integer, String.t, String.t) :: {:ok, map} | {:error, any}
|
||||||
def get_user_characters(user_id, access_token) do
|
def get_user_characters(user_id, access_token, region) do
|
||||||
case Cachex.get(:myapp, "usr_char:#{user_id}") do
|
case Cachex.get(:myapp, cache_key(user_id, region)) do
|
||||||
{:ok, data} -> {:ok, data}
|
{:ok, data} ->
|
||||||
|
{:ok, data}
|
||||||
{:missing, _} ->
|
{:missing, _} ->
|
||||||
access_token
|
access_token
|
||||||
|> resource_url("wow/user/characters")
|
|> resource_url("wow/user/characters", region)
|
||||||
|> HTTPoison.get
|
|> HTTPoison.get
|
||||||
|> parse_character_response(user_id)
|
|> parse_character_response(user_id, region)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp parse_character_response({:error, error}, _), do: {:error, error}
|
defp parse_character_response({:error, error}, _, _), do: {:error, error}
|
||||||
defp parse_character_response({:ok, %HTTPoison.Response{body: body}}, user_id) do
|
defp parse_character_response({:ok, %HTTPoison.Response{body: body}}, user_id, region) do
|
||||||
case Poison.decode(body) do
|
case Poison.decode(body) do
|
||||||
{:ok, data} ->
|
{:ok, data} ->
|
||||||
# only cache end point if characters return
|
# only cache end point if characters return
|
||||||
if (!data["characters"]) do
|
if (!data["characters"]) do
|
||||||
{:error, data}
|
{:error, data}
|
||||||
else
|
else
|
||||||
Cachex.set(:myapp, "usr_char:#{user_id}", data, ttl: :timer.minutes(10)) # 10 minutes
|
Cachex.set(:myapp, cache_key(user_id, region), data, ttl: :timer.minutes(10)) # 10 minutes
|
||||||
end
|
end
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
{:error, error} -> {:error, error}
|
{:error, error} -> {:error, error}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp resource_url(access_token, path) do
|
defp resource_url(access_token, path, region) do
|
||||||
"#{api_url()}/#{path}?access_token=#{access_token}"
|
"#{api_url(region)}/#{path}?access_token=#{access_token}"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ defmodule MyAppWeb.PageController do
|
|||||||
# cache index.html if prod
|
# cache index.html if prod
|
||||||
file = case System.get_env("MIX_ENV") do
|
file = case System.get_env("MIX_ENV") do
|
||||||
:prod ->
|
:prod ->
|
||||||
file = Cachex.get(:myapp, "index.html")
|
Cachex.get(:myapp, "index.html")
|
||||||
|> get_file
|
|> get_file
|
||||||
_ -> File.read!(Application.app_dir(:myapp, "priv/static/index.html"))
|
_ -> File.read!(Application.app_dir(:myapp, "priv/static/index.html"))
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -34,22 +34,6 @@ defmodule MyAppWeb.ThreadController do
|
|||||||
|> Response.json(output)
|
|> Response.json(output)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec update(map, map) :: any
|
|
||||||
def update(conn, params) do
|
|
||||||
user_id = conn
|
|
||||||
|> MyApp.Guardian.Plug.current_claims
|
|
||||||
|> Map.get("id")
|
|
||||||
|
|
||||||
{output, status} = params
|
|
||||||
|> Map.put("user_id", user_id)
|
|
||||||
|> Data.Thread.user_update
|
|
||||||
|> Response.put_resp
|
|
||||||
|
|
||||||
conn
|
|
||||||
|> put_status(status)
|
|
||||||
|> Response.json(output)
|
|
||||||
end
|
|
||||||
|
|
||||||
@spec get_collection(map, map) :: any
|
@spec get_collection(map, map) :: any
|
||||||
def get_collection(conn, params) do
|
def get_collection(conn, params) do
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ defmodule MyAppWeb.UserController do
|
|||||||
alias MyApp.Data
|
alias MyApp.Data
|
||||||
alias MyApp.Guardian.Auth
|
alias MyApp.Guardian.Auth
|
||||||
|
|
||||||
# https://us.battle.net/oauth/authorize?redirect_uri=https://localhost/api/battlenet/authorize&scope=wow.profile&client_id=vxqv32fddxsy6cmk6259amtymbuzmfrq&response_type=code
|
|
||||||
|
|
||||||
# this is for auth with username/password - currently only for admin
|
# this is for auth with username/password - currently only for admin
|
||||||
def login(conn, params) do
|
def login(conn, params) do
|
||||||
{output, status} = params
|
{output, status} = params
|
||||||
@@ -25,7 +23,11 @@ defmodule MyAppWeb.UserController do
|
|||||||
|
|
||||||
{output, status} = code
|
{output, status} = code
|
||||||
|> BattleNet.Auth.get_access_token
|
|> BattleNet.Auth.get_access_token
|
||||||
|> BattleNet.User.get_user
|
# TODO: support for other regions maybe?
|
||||||
|
# right now a US user can auth with the EU end point
|
||||||
|
# maybe it works vice versa? Unable to test it out
|
||||||
|
# because I don't have a test eu account
|
||||||
|
|> BattleNet.User.get_user("us")
|
||||||
|> Data.User.upsert_user
|
|> Data.User.upsert_user
|
||||||
|> Auth.Token.add_token_and_map_claims
|
|> Auth.Token.add_token_and_map_claims
|
||||||
|> Response.put_resp
|
|> Response.put_resp
|
||||||
@@ -35,13 +37,15 @@ defmodule MyAppWeb.UserController do
|
|||||||
|>Response.json(output)
|
|>Response.json(output)
|
||||||
end
|
end
|
||||||
|
|
||||||
def characters(conn, _params) do
|
def characters(conn, params) do
|
||||||
|
region = Map.get(params, "region")
|
||||||
|
|
||||||
%{"access_token" => token, "id" => user_id} = conn
|
%{"access_token" => token, "id" => user_id} = conn
|
||||||
|> MyApp.Guardian.Plug.current_claims
|
|> MyApp.Guardian.Plug.current_claims
|
||||||
|> Map.take(["access_token", "id"])
|
|> Map.take(["access_token", "id"])
|
||||||
|
|
||||||
{output, status} = user_id
|
{output, status} = user_id
|
||||||
|> BattleNet.User.get_user_characters(token)
|
|> BattleNet.User.get_user_characters(token, region)
|
||||||
|> Response.put_resp
|
|> Response.put_resp
|
||||||
|
|
||||||
conn
|
conn
|
||||||
@@ -55,7 +59,7 @@ defmodule MyAppWeb.UserController do
|
|||||||
|> Map.take(["id", "access_token"])
|
|> Map.take(["id", "access_token"])
|
||||||
|
|
||||||
# validate the character exists in users WoW profile
|
# validate the character exists in users WoW profile
|
||||||
{:ok, characterList} = BattleNet.User.get_user_characters(user_id, access_token)
|
{:ok, characterList} = BattleNet.User.get_user_characters(user_id, access_token, params["region"])
|
||||||
exists = Enum.find(characterList["characters"], fn(char) ->
|
exists = Enum.find(characterList["characters"], fn(char) ->
|
||||||
char["name"] == params["character_name"] && char["realm"] == params["character_realm"]
|
char["name"] == params["character_name"] && char["realm"] == params["character_realm"]
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ defmodule MyAppWeb.Router do
|
|||||||
# authenticated routes
|
# authenticated routes
|
||||||
pipe_through [:user_auth]
|
pipe_through [:user_auth]
|
||||||
post "/", ThreadController, :insert
|
post "/", ThreadController, :insert
|
||||||
put "/", ThreadController, :update
|
|
||||||
|
|
||||||
pipe_through [:mod_auth]
|
pipe_through [:mod_auth]
|
||||||
put "/mod", ThreadController, :mod_update
|
put "/mod", ThreadController, :mod_update
|
||||||
|
|||||||
Reference in New Issue
Block a user