diff --git a/examples/avalon/hathora.yml b/examples/avalon/hathora.yml index 342b0666..f0c7294c 100644 --- a/examples/avalon/hathora.yml +++ b/examples/avalon/hathora.yml @@ -74,6 +74,8 @@ auth: nickname: {} google: clientId: 848412826788-m4msrb6q44dm2ue3kgvui0fq7kda55ls.apps.googleusercontent.com + email: + magicPublicApiKey: pk_live_C51086447ADD349D userState: PlayerState error: string diff --git a/src/generate.ts b/src/generate.ts index 8205019c..1c721dbf 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -16,6 +16,7 @@ const HathoraConfig = z anonymous: z.optional(z.object({ separator: z.optional(z.string()).default("-") }).strict()), nickname: z.optional(z.object({}).strict()), google: z.optional(z.object({ clientId: z.string() }).strict()), + email: z.optional(z.object({ magicPublicApiKey: z.string() }).strict()), }) .strict(), userState: z.string(), diff --git a/templates/base/api/base.ts.hbs b/templates/base/api/base.ts.hbs index 0eaa2c21..0716a54e 100644 --- a/templates/base/api/base.ts.hbs +++ b/templates/base/api/base.ts.hbs @@ -51,6 +51,8 @@ export interface {{capitalize @key}}UserData { email: string; locale: string; picture: string; +{{else if (eq @key "email")}} + email: string; {{/if}} } {{/each}} @@ -62,18 +64,18 @@ export function lookupUser(userId: T.UserId): Promise { export function getUserDisplayName(user: UserData) { switch (user.type) { - {{#each auth}} +{{#each auth}} case "{{@key}}": - {{#if (eq @key "anonymous")}} +{{#if (eq @key "anonymous")}} return user.name; - {{else if (eq @key "nickname")}} +{{else if (eq @key "nickname")}} return user.name; - {{else if (eq @key "google")}} +{{else if (eq @key "google")}} return user.name; - {{else if (eq @key "email")}} +{{else if (eq @key "email")}} return user.email; - {{/if}} - {{/each}} +{{/if}} +{{/each}} } } {{#*inline "renderTypeArg"}} diff --git a/templates/base/client/.hathora/client.ts.hbs b/templates/base/client/.hathora/client.ts.hbs index 6895e700..79145cf7 100644 --- a/templates/base/client/.hathora/client.ts.hbs +++ b/templates/base/client/.hathora/client.ts.hbs @@ -51,6 +51,12 @@ export class HathoraClient { return res.data.token; } +{{else if (eq @key "email")}} + public async loginEmail(didToken: string): Promise { + const res = await axios.post(`https://${COORDINATOR_HOST}/${this.appId}/login/email`, { didToken }); + return res.data.token; + } + {{/if}} {{/each}} public async create(token: string, request: IInitializeRequest): Promise { diff --git a/templates/base/client/.hathora/package.json b/templates/base/client/.hathora/package.json.hbs similarity index 100% rename from templates/base/client/.hathora/package.json rename to templates/base/client/.hathora/package.json.hbs diff --git a/templates/base/client/prototype-ui/Login.tsx.hbs b/templates/base/client/prototype-ui/Login.tsx.hbs index 6a136aa6..d2ada7d8 100644 --- a/templates/base/client/prototype-ui/Login.tsx.hbs +++ b/templates/base/client/prototype-ui/Login.tsx.hbs @@ -1,3 +1,6 @@ +{{#if auth.email}} +import { Magic } from "magic-sdk"; +{{/if}} import React, { useState } from "react"; import { toast } from "react-toastify"; {{#if auth.google}} @@ -6,7 +9,13 @@ import { GoogleLogin } from "react-google-login"; import { HathoraClient } from "../.hathora/client"; export function Login({ client, setToken }: { client: HathoraClient; setToken: (token: string) => void }) { + {{#if auth.nickname}} const [nickname, setNickname] = useState(""); + {{/if}} + {{#if auth.email}} + const [email, setEmail] = useState(""); + const magic = new Magic("{{auth.email.magicPublicApiKey}}"); + {{/if}} return (
@@ -72,6 +81,40 @@ export function Login({ client, setToken }: { client: HathoraClient; setToken: ( onFailure={(error) => toast.error("Authentication error: " + error.details)} />
+{{else if (eq @key "email")}} +
+ setEmail(e.target.value)} + className="mr-2 border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" + /> + +
{{/if}} {{/each}}
diff --git a/templates/base/client/prototype-ui/package.json.hbs b/templates/base/client/prototype-ui/package.json.hbs index cfa50a60..51c85fba 100644 --- a/templates/base/client/prototype-ui/package.json.hbs +++ b/templates/base/client/prototype-ui/package.json.hbs @@ -3,6 +3,9 @@ "dependencies": { "@headlessui/react": "1.4.3", "@heroicons/react": "1.0.5", + {{#if auth.email}} + "magic-sdk": "8.1.1", + {{/if}} "react": "17.0.2", "react-dom": "17.0.2", {{#if auth.google}} diff --git a/templates/base/server/.hathora/protocol.ts.hbs b/templates/base/server/.hathora/protocol.ts.hbs index a4df6485..5c4f0a2f 100644 --- a/templates/base/server/.hathora/protocol.ts.hbs +++ b/templates/base/server/.hathora/protocol.ts.hbs @@ -51,6 +51,8 @@ export function register(store: Store): Promise { nickname: {}, {{else if (eq @key "google")}} google: { clientId: "{{clientId}}" }, +{{else if (eq @key "email")}} + email: { secretApiKey: process.env.MAGIC_SECRET_KEY! }, {{/if}} {{/each}} },