Skip to content

Commit 7a8453f

Browse files
committed
Added Profile Edit option
1 parent a490af8 commit 7a8453f

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

components/setting.jsx

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ const ProfileSection = () => {
1717

1818
const handleSubmit = async (event) => {
1919
event.preventDefault();
20-
if (isUpdating) return;
21-
setIsUpdating(true);
20+
21+
console.log(nameRef.current.value);
22+
2223
const formData = new FormData();
24+
2325
if (profilePictureRef.current.files[0]) { formData.append('profilePicture', profilePictureRef.current.files[0]); }
2426
formData.append('name', nameRef.current.value);
2527
formData.append('bio', bioRef.current.value);
28+
29+
2630
const res = await fetch('/api/user', {
2731
method: 'PATCH',
28-
body: formData,
32+
body:formData,
2933
});
3034
if (res.status === 200) {
3135
const userData = await res.json();
@@ -63,12 +67,6 @@ const ProfileSection = () => {
6367
}
6468
};
6569

66-
async function sendVerificationEmail() {
67-
await fetch('/api/user/email/verify', {
68-
method: 'POST',
69-
});
70-
}
71-
7270
return (
7371
<>
7472
<Head>
@@ -78,17 +76,6 @@ const ProfileSection = () => {
7876
<h2>Edit Profile</h2>
7977
{msg.message ? <p style={{ color: msg.isError ? 'red' : '#0070f3', textAlign: 'center' }}>{msg.message}</p> : null}
8078
<form onSubmit={handleSubmit} className="col s12">
81-
{!user.emailVerified ? (
82-
<p>
83-
Your email has not been verify.
84-
{' '}
85-
{/* eslint-disable-next-line */}
86-
<a role="button" className="blue-text" onClick={sendVerificationEmail}>
87-
Send verification email
88-
</a>
89-
</p>
90-
) : null}
91-
9279
<div className="divider"></div>
9380
<label htmlFor="name" className="input-field col s6">
9481
Name
@@ -113,8 +100,8 @@ const ProfileSection = () => {
113100
/>
114101
</label>
115102
<label htmlFor="avatar">
116-
<span>Profile picture &nbsp; </span>
117-
<div className="btn-small file-field input-field blue">
103+
<span>Profile picture &nbsp; </span>
104+
<div className="btn-small file-field input-field blue">
118105
<span>Choose file</span>
119106
<input
120107
type="file"
@@ -126,7 +113,7 @@ const ProfileSection = () => {
126113
</div>
127114
</label>
128115
<br />
129-
<button disabled={isUpdating} type="submit" className="btn blue">Save</button>
116+
<button type="submit" className="btn blue">Save</button>
130117
</form>
131118
<form onSubmit={handleSubmitPasswordChange}>
132119
<label htmlFor="oldpassword">

lib/fetch.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

next.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ require('dotenv').config();
33
module.exports = {
44
env: {
55
MONGODB_URI:process.env.MONGODB_URI,
6-
MONGODB_DB:process.env.MONGODB_DB
6+
MONGODB_DB:process.env.MONGODB_DB,
7+
CLOUDINARY_URL:process.env.CLOUDINARY_URL
78
}
89
}
910

pages/api/user/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
import nextConnect from 'next-connect';
22
import middleware from '../../../middlewares/middleware';
33
import { extractUser } from '../../../lib/api-helpers';
4+
import multer from 'multer';
5+
import { v2 as cloudinary } from 'cloudinary'
46

7+
const upload = multer({ dest: '/tmp' });
58
const handler = nextConnect();
9+
10+
const {
11+
hostname: cloud_name,
12+
username: api_key,
13+
password: api_secret,
14+
} = new URL(process.env.CLOUDINARY_URL);
15+
16+
cloudinary.config({
17+
cloud_name,
18+
api_key,
19+
api_secret,
20+
});
21+
622
handler.use(middleware);
723
handler.get(async (req, res) => res.json({ user: extractUser(req) }));
824

25+
handler.patch(upload.single('profilePicture'), async (req, res) => {
26+
if (!req.user) {
27+
req.status(401).end();
28+
return;
29+
}
30+
31+
let profilePicture;
32+
if (req.file) {
33+
const image = await cloudinary.uploader.upload(req.file.path, {
34+
width: 512,
35+
height: 512,
36+
crop: 'fill',
37+
folder: 'Nextjs-Mongodb-Authentication-App',
38+
use_filename: true
39+
});
40+
profilePicture = image.secure_url;
41+
}
42+
43+
const { name, bio } = req.body;
44+
45+
await req.db.collection('users').updateOne(
46+
{ _id: req.user._id },
47+
{
48+
$set: {
49+
name: name,
50+
bio: bio,
51+
profilePicture: profilePicture
52+
},
53+
},
54+
);
55+
res.json({ user: { name, bio } });
56+
});
57+
58+
export const config = {
59+
api: {
60+
bodyParser: false,
61+
},
62+
};
63+
964
export default handler;

0 commit comments

Comments
 (0)