Skip to content

Commit c4c2512

Browse files
authored
Email invite, upgrade temp user (#259)
* Now we see it * Need to do the agent in the GH action * functioning demo * touches * touchup * links * links * unused * noted todo * Use env variables instead of hard coded links * Use env variables instead of hard coded links
1 parent 0fd815b commit c4c2512

4 files changed

Lines changed: 54 additions & 29 deletions

File tree

auth/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,28 @@ function auth0Middleware() {
5555
const uid = agent.split("id/")[1]
5656
const user = new User(uid)
5757
user.getSelf().then(async (u) => {
58-
if (u?.profile) {
59-
req.user = u
60-
next()
61-
return
62-
}
6358
user.data = {
6459
_id: uid,
6560
agent,
6661
_sub: payload.sub,
6762
email: payload.name,
6863
profile: { displayName: payload.nickname },
6964
}
70-
user.save()
71-
req.user = user
65+
if(!u || !u?.profile) {
66+
user.save()
67+
req.user = user
68+
next()
69+
return
70+
}
71+
if(u?.inviteCode || u._sub.includes("temp-")) {
72+
user.update()
73+
req.user = user
74+
next()
75+
return
76+
}
77+
req.user = u
7278
next()
79+
return
7380
})
7481
} catch (error) {
7582
next(error)

classes/Project/Project.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,37 @@ export default class Project {
6363
const roles = this.parseRoles(rolesString)
6464
const projectTitle = this.data?.label ?? this.data?.title ?? 'TPEN Project'
6565
let message = `You have been invited to the TPEN project ${projectTitle}.
66-
View project <a href='https://three.t-pen.org/project/${this.data._id}'>here</a>.`
66+
View project <a href='${process.env.TPENINTERFACES}project/${this.data._id}'>here</a>.`
6767
if (user) {
6868
await this.inviteExistingTPENUser(user._id, roles)
69-
} else {
70-
const inviteCode = await this.inviteNewTPENUser(email, roles)
71-
// We will replace this URL with the correct url
72-
const url = `https://three.t-pen.org/login?invite-code=${inviteCode}`
73-
message += `<p>Click the button below to get started with your project</p>
74-
<button class = "buttonStyle" ><a href=${url} >Get Started</a> </button>
75-
or copy the following link into your web browser <a href=${url}>${url}</a> </p>`
69+
}
70+
else {
71+
const inviteData = await this.inviteNewTPENUser(email, roles)
72+
const returnTo = encodeURIComponent(`${process.env.TPENINTERFACES}project?projectID=${this.data._id}&inviteCode=${inviteData.tpenUserID}`)
73+
// Signup starting at the TPEN3 public site
74+
const signup = `${process.env.TPENTHREE}login
75+
?inviteCode=${inviteData.tpenUserID}
76+
&returnTo=${returnTo}
77+
`
78+
// TODO decline endpoint in TPEN Services
79+
const decline = `${process.env.TPENINTERFACES}project/decline
80+
?inviteCode=${inviteData.tpenUserID}
81+
&groupID=${inviteData.tpenGroupID}
82+
`
83+
message += `
84+
<p>
85+
Click the button below to get started with your project</p>
86+
<button class="buttonStyle" ><a href="${signup}">Get Started</a></button>
87+
or copy the following link into your web browser <a href="${signup}">${signup}</a>
88+
</p>
89+
<p>
90+
This E-mail address may be visible to members of the project so that they know
91+
about the potential of new members. You may decline this invitation which will keep
92+
you out of the project and remove the visibility of this E-mail address from project details. <br>
93+
<a href="${decline}">Click here to decline the invitation.</a>
94+
</p>
95+
`
7696
}
77-
7897
await sendMail(email, `Invitation to ${projectTitle}`, message)
7998
return this
8099
} catch (error) {
@@ -133,15 +152,14 @@ export default class Project {
133152

134153
async inviteNewTPENUser(email, roles) {
135154
const user = new User()
136-
const inviteCode = this.#generateInviteCode(user._id)
155+
const inviteCode = user._id
137156
const agent = `https://store.rerum.io/v1/id/${user._id}`
138157
const profile = { displayName: email.split("@")[0] }
139158
const _sub = `temp-${user._id}` // This is a temporary sub for the user until they verify their email
140159
user.data = { email, _sub, profile, agent, inviteCode }
141160
await user.save()
142161
await this.inviteExistingTPENUser(user._id, roles)
143-
144-
return inviteCode
162+
return { "tpenUserID":user._id, "tpenGroupID":this.data.group }
145163
}
146164

147165
async removeMember(userId) {
@@ -238,15 +256,6 @@ export default class Project {
238256
return await database.save(this.data, process.env.TPENPROJECTS)
239257
}
240258

241-
#generateInviteCode(userId) {
242-
const date = Date.now().toString()
243-
const data = `${date}:${userId}`
244-
245-
const hash = createHash("sha256")
246-
hash.update(data)
247-
return hash.digest("hex")
248-
}
249-
250259
async #load() {
251260
return database.getById(this._id, "projects").then((resp) => {
252261
this.data = resp

classes/User/User.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ export default class User {
123123
*/
124124

125125
async update(){
126+
if (!this._id) {
127+
throw new Error("User must have an _id")
128+
}
129+
if (!this.data.email) {
130+
throw new Error("User must have an email")
131+
}
132+
if (!this.data.profile?.displayName) {
133+
throw new Error("User must have a profile with a displayName")
134+
}
126135
return database.update(this.data, "users")
127136
}
128137

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function makeCleanFileFromMarkdown(file) {
6262
<head>
6363
<meta charset="UTF-8">
6464
<meta name="viewport" content="width=device-width, initial-scale=1.0">
65-
<link rel="stylesheet" href="https://three.t-pen.org/assets/css/main.css">
65+
<link rel="stylesheet" href="${process.env.TPENTHREE}assets/css/main.css">
6666
<title>API Documentation</title>
6767
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
6868
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>

0 commit comments

Comments
 (0)