Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e878a35

Browse files
committed
Figure out firstname and last name automatically from the name we get from GitHub
1 parent b445f04 commit e878a35

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/adapters/fauna/clear-db.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ async function main() {
3939
q.Paginate(q.Documents(q.Collection('goal_updates'))),
4040
(goalUpdate) => q.Delete(goalUpdate)
4141
),
42-
q.Map(q.Paginate(q.Documents(q.Collection('goal_likes'))), (goalLike) =>
43-
q.Delete(goalLike)
42+
q.Map(
43+
q.Paginate(q.Documents(q.Collection('update_likes'))),
44+
(updateLike) => q.Delete(updateLike)
4445
),
4546
q.Map(
4647
q.Paginate(q.Documents(q.Collection('update_comments'))),

src/adapters/fauna/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const Adapter = (config, options = {}) => {
4242
email: profile.email,
4343
image: profile.image,
4444
account: {
45-
firstName: profile.name,
45+
firstName: profile.firstName,
46+
lastName: profile.lastName,
4647
bio: profile.bio,
4748
},
4849
socials: {
@@ -165,7 +166,8 @@ const Adapter = (config, options = {}) => {
165166
email: user.email,
166167
image: user.image,
167168
account: {
168-
firstName: user.name,
169+
firstName: user.firstName,
170+
lastName: user.lastName,
169171
bio: user.bio,
170172
},
171173
socials: {

src/pages/api/auth/[...nextauth].ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ const faunaClient = new faunadb.Client({
1717
const authHandler: NextApiHandler = (req, res) => NextAuth(req, res, options)
1818
export default authHandler
1919

20+
function getFirstAndLastNames(name: string) {
21+
const nameArray = name.split(' ')
22+
let firstName = ''
23+
let lastName = ''
24+
if (nameArray.length === 1) {
25+
firstName = name
26+
} else if (nameArray.length > 1) {
27+
firstName = nameArray.slice(0, -1).join(' ')
28+
lastName = nameArray.slice(-1).join(' ')
29+
}
30+
return { firstName, lastName }
31+
}
32+
2033
const options: InitOptions = {
2134
providers: [
2235
Providers.GitHub({
@@ -25,9 +38,13 @@ const options: InitOptions = {
2538
scope: 'user:email',
2639
// @ts-ignore
2740
profile: (profileData) => {
41+
const name = profileData.name || profileData.login
42+
const { firstName, lastName } = getFirstAndLastNames(name)
2843
return {
2944
id: profileData.id,
30-
name: profileData.name || profileData.login,
45+
name,
46+
firstName,
47+
lastName,
3148
email: profileData.email,
3249
image: profileData.avatar_url,
3350
username: profileData.login,
@@ -72,6 +89,8 @@ const options: InitOptions = {
7289
id: profileData.id,
7390
name,
7491
email: null,
92+
firstName: profileData.localizedFirstName,
93+
lastName: profileData.localizedLastName,
7594
image: profileImage,
7695
username,
7796
bio: null,

0 commit comments

Comments
 (0)