Slide 1

Slide 1 text

Go beyound static on Netlify 2018/09/26 Nelify Tokyo @joe_re

Slide 2

Slide 2 text

Who am I? twitter: @joe_re github: @joe­re ClassDo Inc CTO community: Nishinippori.rb, GraphQL Tokyo Organizer

Slide 3

Slide 3 text

Netlify

Slide 4

Slide 4 text

What do you feel good points of Netlify?

Slide 5

Slide 5 text

You don't need to call infra engineers for hosting your website. Easy to setup your project's deploy and CI (just takes few clicks!) Automatic https Easy to set custom domain

Slide 6

Slide 6 text

Integrate your project's repository

Slide 7

Slide 7 text

Netlify is not only for static sites

Slide 8

Slide 8 text

Demo Admin page for our product

Slide 9

Slide 9 text

Demo application's raugh image of architecture

Slide 10

Slide 10 text

We use Netlify functions like a BFF https://docs.microsoft.com/en­ us/azure/architecture/patterns/backends­for­frontends

Slide 11

Slide 11 text

Lumbda functions on Netlify You can deploy Lambda functions without your AWS account You don't need to setup to publish endpoint (it'll be enable to handle request automatically via API gateway)

Slide 12

Slide 12 text

Setup functions Web Interface or Netlify.toml [build] functions = "./functions"

Slide 13

Slide 13 text

Functions example project_root/functions/hello.js exports.handler = function(event, context, callback) { callback(null, { statusCode: 200, body: "Hello, World" }); } $ curl http://localhost:8080/.netlify/functions/hello Hello, World%

Slide 14

Slide 14 text

Netlify Identify You can manage and authenticate users on Netlify Context You can use this just click enable button (you don't need to create a database, deploy other modules or others..) Based on JWT

Slide 15

Slide 15 text

Authentication by Netlify Identify Netlify Identity widget https://github.com/netlify/netlify­identity­widget Lower level client libraly https://github.com/netlify/gotrue­js import GoTrue from "gotrue-js"; auth = new GoTrue({ APIUrl: "https:///.netlify/identity", audience: "", setCookie: false }); auth.signup(email, password);

Slide 16

Slide 16 text

Set roles to user metadata Web Interface

Slide 17

Slide 17 text

Set roles to user metadata On Functions(by admin methods) exports.handler = async (event, context) => { const { identity, user } = context.clientContext; const userID = user.sub; const userUrl = `${identity.url}/admin/users/${userID}`; const adminAuthHeader = "Bearer " + identity.token; try { return fetch(userUrl, { method: "PUT", headers: { Authorization: adminAuthHeader }, body: JSON.stringify({ app_metadata: { roles: ["superstar"] } }) }) .then(response => { return response.json(); }) .then(data => { console.log("Updated a user! 204!"); console.log(JSON.stringify({ data })); return { statusCode: 204 }; }) .catch(e => return {...}); } catch (e) { return e; } };

Slide 18

Slide 18 text

Identity and Functions export function(event, context, callback) { const {identity, user} = context.clientContext; // Do stuff... } context.clientContext includes identify and user property. Identify has information to request admin methods. User has information of authenticated user.

Slide 19

Slide 19 text

Example: Check user role in your functions const allowRoles = ['superstar'] if (!allowRoles.some(role => context.clientContext.user.app_metadata.roles.includes(role))) { callback(null, { statusCode: 401, body: JSON.stringify({ message: 'Not authorized' }) } } //...

Slide 20

Slide 20 text

You can set different environments each stages netlify.toml # Production context [context.production] environment = { ACCESS_TOKEN = "super secret", NODE_ENV = "8.0.1" } # Deploy Preview context [context.deploy-preview.environment] environment = { ACCESS_TOKEN: "not so secret" } # Specific branch context [context.staging] # 'staging' is a branch name environment = { ACCESS_TOKEN: "stg secret" }

Slide 21

Slide 21 text

Local Development netlify­lambda https://github.com/netlify/netlify­lambda read netlify.toml and build or launce server for functions $ npx netlify-lambda serve functions Starting server Function source and publish folder should be in different locations Lambda server is listening on 9000 Request from 127.0.0.1: GET /test Response with status 200 in 4 ms.

Slide 22

Slide 22 text

Conclusion

Slide 23

Slide 23 text

Today I took about Netlify features for Go beyound static on Netlify.

Slide 24

Slide 24 text

What do you think the best point of Netlify Go beyound static stacks are?

Slide 25

Slide 25 text

I think the best point is you can create and maintain that by only frontend enginners contexts.

Slide 26

Slide 26 text

We can provide our product quickly without any long way thanks to Netlify stacks

Slide 27

Slide 27 text

Thank you for your attention!