Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cab2866

Browse files
Added login and save function
1 parent 97465b6 commit cab2866

File tree

6 files changed

+362
-30
lines changed

6 files changed

+362
-30
lines changed

‎functions/save.js

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,48 @@
11
const AWS = require("aws-sdk");
22

33
exports.handler = function(event, context, callback) {
4-
// const { identity, user } = context.clientContext;
5-
6-
// Configure client for use with Spaces
7-
const spacesEndpoint = new AWS.Endpoint(process.env.S3_ENDPOINT);
8-
const s3 = new AWS.S3({
9-
endpoint: spacesEndpoint,
10-
accessKeyId: process.env.S3_KEY,
11-
secretAccessKey: process.env.S3_SECRET,
12-
});
13-
14-
// Add a file to a Space
15-
var params = {
16-
Body: "The contents of the file",
17-
Bucket: "netlify-spaces",
18-
Key: "data.json",
19-
};
20-
21-
s3.putObject(params, function(err, data) {
22-
if (err) console.log(err, err.stack);
23-
else console.log(data);
24-
});
25-
26-
callback(null, {
27-
statusCode: 200,
28-
body: JSON.stringify({ foo: "bar" }),
29-
});
4+
try {
5+
const { user } = context.clientContext;
6+
7+
if (!user) throw new Error("Not Authorized");
8+
9+
if (event.httpMethod !== "POST") {
10+
return {
11+
statusCode: 405,
12+
body: "Method Not Allowed",
13+
};
14+
}
15+
16+
const jsondata = JSON.parse(event.body);
17+
18+
// Configure client for use with Spaces
19+
const spacesEndpoint = new AWS.Endpoint(process.env.S3_ENDPOINT);
20+
const s3 = new AWS.S3({
21+
endpoint: spacesEndpoint,
22+
accessKeyId: process.env.S3_KEY,
23+
secretAccessKey: process.env.S3_SECRET,
24+
});
25+
26+
// Add a file to a Space
27+
var params = {
28+
Body: JSON.stringify(jsondata),
29+
Bucket: "netlify-spaces",
30+
Key: "data.json",
31+
};
32+
33+
s3.putObject(params, function(err, data) {
34+
if (err) console.log(err, err.stack);
35+
else console.log(data);
36+
});
37+
38+
callback(null, {
39+
statusCode: 200,
40+
body: JSON.stringify({ foo: "bar" }),
41+
});
42+
} catch (error) {
43+
return {
44+
statusCode: 401,
45+
body: "Not Authorized.",
46+
};
47+
}
3048
};

‎public/index.html

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html>
3+
34
<head>
4-
<meta charset="UTF-8">
5-
<title>Untitled Document</title>
5+
<meta charset="UTF-8" />
6+
<title>Untitled Document</title>
7+
8+
<!-- Netlify Identity Widget -->
9+
<script type="text/javascript" src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
610
</head>
711

812
<body>
13+
<div class="container text-center">
14+
<button class="btn btn-outline-dark" id="save" style="display: none;">
15+
Save
16+
</button>
17+
</div>
18+
<div class="container" id="result"></div>
19+
20+
<script>
21+
const saveBut = document.querySelector('#save');
22+
23+
netlifyIdentity.on("login", function(user) {
24+
console.log(user);
25+
saveBut.style.display = 'block';
26+
saveBut.addEventListener('click', function() {
27+
save();
28+
})
29+
});
30+
31+
32+
function save() {
33+
const token = await netlifyIdentity.currentUser().jwt();
34+
let user = await netlifyIdentity.currentUser();
35+
36+
let data = {
37+
title: "hello world"
38+
}
39+
40+
const response = await fetch("/.netlify/functions/save", {
41+
headers: {
42+
Authorization: `Bearer ${token}`,
43+
},
44+
method: "post",
45+
body: JSON.stringify({
46+
data: data,
47+
}),
48+
}).then((res) => res.text());
49+
}
50+
</script>
951
</body>
10-
</html>
52+
53+
</html>

‎public/login/index.html

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Login</title>
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
8+
9+
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
10+
<style>
11+
.wrapper {
12+
width: 320px;
13+
margin: 0 auto;
14+
}
15+
16+
.container {
17+
margin-top: 50px;
18+
margin-bottom: 100px;
19+
}
20+
21+
button {
22+
margin-top: 25px;
23+
}
24+
</style>
25+
26+
<!-- Netlify Identity Widget -->
27+
<script type="text/javascript" src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
28+
29+
<!-- Markdown parser, just for this example -->
30+
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js"></script>
31+
32+
</head>
33+
34+
<body>
35+
36+
<div class="wrapper mt-5">
37+
<div class="card">
38+
<div class="card-body text-center">
39+
<h5 class="card-title">Log In</h5>
40+
<p class="card-text">Log in with Netlify Identity</p>
41+
<button class="btn btn-outline-dark w-100" id="login">Log In / Register</button>
42+
</div>
43+
</div>
44+
</div>
45+
46+
<div class="container" id="result"></div>
47+
48+
<script type="text/javascript" src="js/login.js"></script>
49+
50+
</body>
51+
52+
</html>

‎public/login/js/login.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
document.querySelector('#login').addEventListener('click', function() {
2+
netlifyIdentity.open()
3+
})
4+
5+
netlifyIdentity.on('login', function(user) {
6+
document.querySelector('#login').innerHTML = 'Log Out'
7+
window.location = '../';
8+
})

‎public/login/login.css

Whitespace-only changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /