Correct authentication webhook and frontend with ethereum_address in Player

This commit is contained in:
Pacien Boisson
2020-07-28 12:03:38 +02:00
committed by Hammad Jutt
parent 9cdfa26a78
commit 84a76ad2fe
9 changed files with 38 additions and 52 deletions

View File

@@ -1,4 +1,4 @@
import { GetMyAccount } from '../graphql/queries';
import { GetPlayerFromAddress } from '../graphql/queries';
const STORAGE_KEY = 'auth-token';
@@ -31,17 +31,17 @@ export async function login(client, token, ethAddress) {
});
return client
.query({
query: GetMyAccount,
variables: { eth_address: ethAddress },
query: GetPlayerFromAddress,
variables: { ethereum_address: ethAddress },
})
.then(async res => {
if (res.data.Account.length === 0) {
if (res.data.Player.length === 0) {
throw new Error('Impossible to fetch player, not found.');
}
client.writeData({
data: {
authState: 'logged',
playerId: res.data.Account[0].Player.id,
playerId: res.data.Player[0].id,
},
});
setTokenInStore(token);

View File

@@ -5,7 +5,6 @@ import React, { useCallback, useEffect, useState } from 'react';
import { useMyPlayer } from '../graphql/hooks';
import { UpdateBoxProfiles, UpdateUsername } from '../graphql/mutations';
import { getPlayerETHAddress } from '../utils/players';
function getProfilePicture(boxProfile: any) {
const imageHash =
@@ -28,7 +27,7 @@ export const PlayerDetails: React.FC<{ player: any }> = ({ player }) => {
const [updateBoxProfiles] = useMutation(UpdateBoxProfiles);
const [updateUsername] = useMutation(UpdateUsername);
const ethAddress = getPlayerETHAddress(player);
const ethAddress = player.ethereum_address;
useEffect(() => {
(async () => {

View File

@@ -4,11 +4,11 @@ import React from 'react';
import { useParams } from 'react-router-dom';
import { PlayerDetails } from '../components/PlayerDetails';
import { GetPlayer } from '../graphql/queries';
import { GetPlayerFromId } from '../graphql/queries';
export const Player: React.FC = () => {
const { playerId } = useParams();
const { data, loading, error } = useQuery(GetPlayer, {
const { data, loading, error } = useQuery(GetPlayerFromId, {
variables: {
player_id: playerId,
},

View File

@@ -3,10 +3,10 @@ import { Box } from '@material-ui/core';
import React from 'react';
import { PlayerListItem } from '../components/PlayerListItem';
import { GetPlayer } from '../graphql/queries';
import { GetPlayerFromId } from '../graphql/queries';
export const PlayerList: React.FC = () => {
const { data, loading, error } = useQuery(GetPlayer);
const { data, loading, error } = useQuery(GetPlayerFromId);
if (error) {
return <div>error: {error.message}</div>;

View File

@@ -2,6 +2,7 @@ export const PlayerFragment = `
fragment PlayerFragment on Player {
id
username
ethereum_address
}
`;
export const AccountFragment = `

View File

@@ -2,11 +2,11 @@ import { useLazyQuery, useQuery } from '@apollo/react-hooks';
import { useEffect } from 'react';
import { localQueries } from '../apollo';
import { GetPlayer } from './queries';
import { GetPlayerFromId } from './queries';
export function useMyPlayer() {
const authStateQuery = useQuery(localQueries.GetAuthState);
const [getMyPlayer, myPlayerQuery] = useLazyQuery(GetPlayer);
const [getMyPlayer, myPlayerQuery] = useLazyQuery(GetPlayerFromId);
const playerId = authStateQuery.data?.playerId;

View File

@@ -2,7 +2,7 @@ import gql from 'graphql-tag';
import { AccountFragment, PlayerFragment } from './fragments';
export const GetPlayer = gql`
export const GetPlayerFromId = gql`
query GetPlayer($player_id: uuid) {
Player(where: { id: { _eq: $player_id } }) {
...PlayerFragment
@@ -15,17 +15,13 @@ export const GetPlayer = gql`
${AccountFragment}
`;
export const GetMyAccount = gql`
query GetMyAccount($eth_address: String) {
Account(
where: { identifier: { _eq: $eth_address }, type: { _eq: "ETHEREUM" } }
export const GetPlayerFromAddress = gql`
query GetMyPlayer($ethereum_address: String) {
Player(
where: { ethereum_address: { _eq: $ethereum_address } }
) {
...AccountFragment
Player {
...PlayerFragment
}
...PlayerFragment
}
}
${PlayerFragment}
${AccountFragment}
`;

View File

@@ -1,3 +0,0 @@
export function getPlayerETHAddress(player: any) {
return player.Accounts.find((a: any) => a.type === 'ETHEREUM').identifier;
}

View File

@@ -1,63 +1,56 @@
import { hasuraQuery } from '../../lib/hasuraHelpers';
const getPlayerQuery = `
query GetPlayerFromETH ($eth_address: String) {
Account(
query GetPlayerFromETH ($ethereum_address: String) {
Player(
where: {
identifier: { _eq: $eth_address },
type: { _eq: "ETHEREUM" }
ethereum_address: { _eq: $ethereum_address }
}
) {
Player {
id
}
id
}
}
`;
const createProfileMutation = `
mutation CreateAccountFromETH ($eth_address: String!, $username: String!) {
insert_Account(
mutation CreateAccountFromETH ($ethereum_address: String!, $username: String!) {
insert_Player(
objects: {
type: "ETHEREUM",
identifier: $eth_address
Player: {
data: {
username: $username
}
}
username: $username,
ethereum_address: $ethereum_address
}) {
affected_rows
returning {
identifier
Player {
id
}
id
username
ethereum_address
}
}
}
`;
interface IPlayer {
id: string;
username: string;
ethereum_address: string;
}
export async function createPlayer(ethAddress: string): Promise<IPlayer> {
const resProfile = await hasuraQuery(createProfileMutation, {
eth_address: ethAddress,
ethereum_address: ethAddress,
username: ethAddress,
});
if (resProfile.insert_Account.affected_rows !== 2) {
throw new Error('error while creating profile');
if (resProfile.insert_Player.affected_rows !== 1) {
throw new Error('Error while creating player');
}
return resProfile.insert_Account.returning[0].Player;
return resProfile.insert_Player.returning[0];
}
export async function getPlayer(ethAddress: string): Promise<IPlayer> {
const res = await hasuraQuery(getPlayerQuery, {
eth_address: ethAddress,
ethereum_address: ethAddress,
});
let player = res.Account[0]?.Player;
let player = res.Player[0];
if (!player) {
player = await createPlayer(ethAddress);