From 1a25b2c6b33822f8b66cd5c7f8412699addff6b0 Mon Sep 17 00:00:00 2001
From: Mathias Biilmann Christensen <info@mathias-biilmann.net>
Date: Tue, 5 Sep 2017 16:48:51 -0700
Subject: [PATCH] Better handling of roles and names

---
 example/config.yml                              |  5 +++--
 example/index.html                              |  3 +++
 src/backends/netlify-auth/AuthenticationPage.js |  4 ----
 src/backends/netlify-auth/implementation.js     | 12 ++++++++----
 4 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/example/config.yml b/example/config.yml
index 464d2c38..c50e4957 100644
--- a/example/config.yml
+++ b/example/config.yml
@@ -1,6 +1,7 @@
 backend:
-  name: test-repo
-  delay: 0.1
+  name: netlify-auth
+  auth_url: "/.netlify/identity"
+  github_proxy_url: "/.netlify/git/github"
 
 media_folder: "assets/uploads"
 
diff --git a/example/index.html b/example/index.html
index 262e554b..c7182f13 100644
--- a/example/index.html
+++ b/example/index.html
@@ -78,6 +78,9 @@
     }
   }
   </script>
+
+  <script type='text/javascript' src='https://d33wubrfki0l68.cloudfront.net/js/1e8330b19f2c7165019dffac65520214da9f0af4/netlify-identity-umd.js'></script>
+	<script>window.identity = new netlifyIdentity()</script>
 </head>
 <body>
 
diff --git a/src/backends/netlify-auth/AuthenticationPage.js b/src/backends/netlify-auth/AuthenticationPage.js
index f8c6e394..9627b39f 100644
--- a/src/backends/netlify-auth/AuthenticationPage.js
+++ b/src/backends/netlify-auth/AuthenticationPage.js
@@ -66,10 +66,6 @@ export default class AuthenticationPage extends React.Component {
     const { errors } = this.state;
     const { error } = this.props;
 
-    if (this.identity) {
-      return <section className={styles.root}></section>;
-    }
-
     return (
       <section className={styles.root}>
         <Card className={styles.card}>
diff --git a/src/backends/netlify-auth/implementation.js b/src/backends/netlify-auth/implementation.js
index 7eda7d93..3de7b402 100644
--- a/src/backends/netlify-auth/implementation.js
+++ b/src/backends/netlify-auth/implementation.js
@@ -36,7 +36,7 @@ export default class NetlifyAuth extends GitHubBackend {
     const netlifySiteURL = localStorage.getItem("netlifySiteURL");
     const APIUrl = getEndpoint(config.getIn(["backend", "auth_url"]), netlifySiteURL);
     this.github_proxy_url = getEndpoint(config.getIn(["backend", "github_proxy_url"]), netlifySiteURL);
-    this.authClient = new Gotrue({APIUrl});
+    this.authClient = new GoTrue({APIUrl});
 
     AuthenticationPage.authClient = this.authClient;
   }
@@ -51,10 +51,14 @@ export default class NetlifyAuth extends GitHubBackend {
     this.tokenPromise = user.jwt.bind(user);
     return this.tokenPromise()
     .then((token) => {
+      let validRole = true;
+      if (this.accept_roles && this.accept_roles.length > 0) {
+        validRole = intersection(userRoles, this.accept_roles).length > 0;
+      }
       const userRoles = get(jwtDecode(token), 'app_metadata.roles', []);
-      if (intersection(userRoles, this.accept_roles).length > 0) {
+      if (validRole) {
         const userData = {
-          name: `${ user.user_metadata.firstname } ${ user.user_metadata.lastname }`,
+          name: user.user_metadata.name,
           email: user.email,
           metadata: user.user_metadata,
         };
@@ -65,7 +69,7 @@ export default class NetlifyAuth extends GitHubBackend {
         });
         return userData;
       } else {
-        throw new Error("User is not authorized");
+        throw new Error("You don't have sufficient permissions to access Netlify CMS");
       }
     });
   }