Skip to content

Commit

Permalink
Merge branch 'master' into blms/issue17
Browse files Browse the repository at this point in the history
  • Loading branch information
blms authored Aug 3, 2020
2 parents cf3c272 + bb7d0d5 commit 238dead
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 254 deletions.
1 change: 1 addition & 0 deletions .env.local.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ EMAIL_SERVER_HOST=smpt.example.net
EMAIL_SERVER_PORT=587
[email protected]
MONGODB_URI=mongodb://localhost:27017/as4
NEXTAUTH_URL=http://localhost:3000
SITE=http://localhost:3000
SITE_NAME=Annotation Studio
479 changes: 261 additions & 218 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"mongo-seeding": "^3.4.1",
"mongo-seeding-cli": "^3.4.1",
"mongodb": "^3.5.9",
"next": "latest",
"next-auth": "^2.2.0",
"next-connect": "^0.8.0",
"next": "^9.5.1",
"next-auth": "^3.0.1",
"next-connect": "^0.8.1",
"nodemailer": "^6.4.10",
"popper.js": "^1.16.1",
"react": "^16.13.1",
Expand Down
7 changes: 6 additions & 1 deletion src/pages/_app.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* eslint-disable react/jsx-props-no-spreading */

import { Provider } from 'next-auth/client';
import '../style/custom.scss';

export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
return (
<Provider session={pageProps.session}>
<Component {...pageProps} />
</Provider>
);
}
37 changes: 25 additions & 12 deletions src/pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-param-reassign */
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
import Adapters from 'next-auth/adapters';
Expand All @@ -6,8 +7,6 @@ import sendVerificationRequestOverride from '../../../utils/verificationUtil';
import Models from '../../../models';

const options = {
site: process.env.SITE || 'http://localhost:3000',

providers: [
Providers.Email({
server: {
Expand All @@ -29,13 +28,13 @@ const options = {
},
}),
],
adapter: Adapters.TypeORM.Adapter({
type: 'mongodb',
url: process.env.MONGODB_URI,
customModels: {
User: Models.User,
},
}),

adapter: Adapters.TypeORM.Adapter(process.env.MONGODB_URI,
{
customModels: {
User: Models.User,
},
}),

session: {
jwt: true,
Expand All @@ -45,23 +44,37 @@ const options = {

secret: process.env.AUTH_SECRET,

jwt: {
secret: process.env.AUTH_SECRET,
raw: true,
},

pages: {
newUser: '/user/newuser',
},

callbacks: {
session: async (session, token) => {
const { id } = token.user;
jwt: async (token, user) => {
const isSignIn = !!(user);
if (isSignIn) {
token.auth_time = Number(new Date());
token.id = user.id;
}
return Promise.resolve(token);
},

session: async (session, sessionToken) => {
const { id } = sessionToken;
const url = `${process.env.SITE}/api/user/${id}`;
const res = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
});
if (res.status === 200) {
const user = await res.json();
// eslint-disable-next-line no-param-reassign
session.user.name = user.name;
} else {
return Promise.reject();
Expand Down
8 changes: 4 additions & 4 deletions src/pages/api/document/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const handler = nc()
.use(middleware)
.get(
async (req, res) => {
const token = await jwt.getJwt({ req, secret });
const token = await jwt.getToken({ req, secret });
if (token && token.exp > 0) {
await req.db
.collection('documents')
.findOne(
{
_id: ObjectID(req.query.id),
$or: [{ 'groups.members.id': ObjectID(token.user.id) }, { owner: ObjectID(token.user.id) }],
$or: [{ 'groups.members.id': ObjectID(token.id) }, { owner: ObjectID(token.id) }],
},
(err, doc) => {
if (doc) {
Expand Down Expand Up @@ -89,7 +89,7 @@ const handler = nc()
)
.patch(
async (req, res) => {
const token = await jwt.getJwt({ req, secret });
const token = await jwt.getToken({ req, secret });
if (token && token.exp > 0) {
const {
title,
Expand Down Expand Up @@ -171,7 +171,7 @@ const handler = nc()
.findOneAndUpdate(
{
_id: ObjectID(req.query.id),
owner: ObjectID(token.user.id),
owner: ObjectID(token.id),
...groupById,
},
{
Expand Down
10 changes: 5 additions & 5 deletions src/pages/api/group/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const handler = nc()
.use(middleware)
.get(
async (req, res) => {
const token = await jwt.getJwt({ req, secret });
const token = await jwt.getToken({ req, secret });
if (token && token.exp > 0) {
await req.db
.collection('groups')
.findOne(
{
_id: ObjectID(req.query.id),
'members.id': ObjectID(token.user.id),
'members.id': ObjectID(token.id),
},
(err, doc) => {
if (doc) {
Expand Down Expand Up @@ -46,11 +46,11 @@ const handler = nc()
)
.patch(
async (req, res) => {
const token = await jwt.getJwt({ req, secret });
const token = await jwt.getToken({ req, secret });
if (token && token.exp > 0) {
const memberQuery = (process.env.NODE_ENV === 'development')
? [{ 'members.id': ObjectID(token.user.id) }, { 'members.id': fakeUserId }]
: [{ 'members.id': ObjectID(token.user.id) }];
? [{ 'members.id': ObjectID(token.id) }, { 'members.id': fakeUserId }]
: [{ 'members.id': ObjectID(token.id) }];
const nameToUpdate = req.body.name
? { name: req.body.name }
: {};
Expand Down
8 changes: 4 additions & 4 deletions src/pages/api/group/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const handler = nc()
.use(middleware)
.post(
async (req, res) => {
const token = await jwt.getJwt({ req, secret });
const token = await jwt.getToken({ req, secret });
if (token && token.exp > 0) {
const { name } = req.body;
const members = [{
id: ObjectID(token.user.id),
name: token.user.name,
email: token.user.email,
id: ObjectID(token.id),
name: token.name,
email: token.email,
role: 'owner',
}];
const documents = [{}];
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/user/slug/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const handler = nc()
.use(middleware)
.get(
async (req, res) => {
const token = await jwt.getJwt({ req, secret });
const token = await jwt.getToken({ req, secret });
if (token && token.exp > 0) {
await req.db
.collection('users')
.findOne(
{ slug: req.query.slug },
(err, doc) => {
if (doc) {
if (doc.email === token.user.email) {
if (doc.email === token.email) {
const {
name, firstName, lastName, affiliation, email,
} = doc;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const handler = nc()
.use(middleware)
.patch(
async (req, res) => {
const token = await jwt.getJwt({ req, secret });
const token = await jwt.getToken({ req, secret });
if (token && token.exp > 0) {
await req.db
.collection('users')
.findOneAndUpdate(
{ email: token.user.email },
{ email: token.email },
{
$set: {
name: req.body.name,
Expand Down
9 changes: 6 additions & 3 deletions src/pages/user/[slug]/editprofile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fullName from '../../../utils/nameUtil';
import Layout from '../../../components/Layout';

const EditProfile = ({ user }) => {
const [session] = useSession();
const [session, loading] = useSession();

const [errorMsg, setErrorMsg] = useState('');

Expand Down Expand Up @@ -53,7 +53,7 @@ const EditProfile = ({ user }) => {
<Layout>
<Col lg="8" className="mx-auto">
<Card>
{!session && (
{!session && loading && (
<Card.Body className="text-center">
<Spinner animation="border" role="status">
<span className="sr-only">Loading...</span>
Expand Down Expand Up @@ -175,7 +175,10 @@ export async function getServerSideProps(context) {
// eslint-disable-next-line no-undef
const res = await fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
Cookie: context.req.headers.cookie,
},
});
if (res.status === 200) {
const foundUser = await res.json();
Expand Down
1 change: 1 addition & 0 deletions src/pages/user/newuser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const NewUser = () => {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
credentials: 'include',
});
if (res.status === 200) {
await res.json();
Expand Down

0 comments on commit 238dead

Please sign in to comment.