Skip to content

Commit e4a6618

Browse files
Resul AvanResul Avan
authored andcommitted
generate username by email/displayName
1 parent 2363823 commit e4a6618

File tree

10 files changed

+58
-38
lines changed

10 files changed

+58
-38
lines changed

src/assets/style.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ footer {
119119

120120
.is-overlay-bottom-left {
121121
position: absolute !important;
122-
bottom: 5px !important;
123-
left: 5px !important;
122+
bottom: 10px !important;
123+
left: 10px !important;
124124
z-index: 3 !important;
125125
}
126126

127127
.is-overlay-bottom-right {
128128
position: absolute !important;
129-
bottom: 5px !important;
130-
right: 5px !important;
129+
bottom: 10px !important;
130+
right: 10px !important;
131131
z-index: 3 !important;
132132
}
133133

src/components/form/SocialLogin.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class SocialLogin extends Vue {
6868
return this.reauthenticate
6969
? await this.reauthenticateWithSocialProvider(this.getSocialLoginCredentials(providerType))
7070
: await this.signInWithSocialProvider(this.getSocialLoginCredentials(providerType))
71-
.then(async () => await reloadUserFromDatabase.next())
71+
.then(() => reloadUserFromDatabase.next())
7272
})
7373
.finally(() => this.saveLoading(false))
7474
}

src/components/profile/header/CoverPhoto.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="container">
3-
<div class="card-image has-cursor-pointer has-max-height-500 crop-to-fit" @click="showLightbox">
3+
<div class="card-image has-cursor-pointer has-max-height-350 crop-to-fit" @click="showLightbox">
44
<img
55
v-lazy="coverPhoto.src"
66
:src="placeholder"

src/components/profile/module/ProfileModule.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="container">
33
<div class="columns is-centered">
44
<div class="column is-half">
5-
<b-tabs v-model="moduleType" type="is-toggle-rounded" size="is-small" expanded>
5+
<b-tabs v-model="moduleType" type="is-toggle-rounded" size="is-small">
66
<b-tab-item
77
v-for="(moduleConfig, key) in computedModuleConfigs"
88
:key="key"

src/pages/register.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default class register extends Vue {
4242
.then(async () => {
4343
await this.clearNotificationMessage()
4444
await this.signUpWithEmail(credentials)
45-
.then(async () => await reloadUserFromDatabase.next())
45+
.then(() => reloadUserFromDatabase.next())
4646
})
4747
.finally(() => this.saveLoading(false))
4848
}

src/plugins/firebase-auth-listener.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ import {
1414
} from '~/types'
1515
import { authenticatedAllowed, authenticatedNotAllowed } from '~/service/global-service'
1616
import { getAuthUser } from '~/service/firebase/firebase-service'
17-
import {
18-
configureAxiosObservable,
19-
configureFcmObservable,
20-
loadNotificationObservable,
21-
reloadUserFromDatabase
22-
} from '~/service/rx-service'
17+
import { configureAxiosObservable, configureFcmObservable, loadNotificationObservable } from '~/service/rx-service'
2318

24-
const logout = (store: Store<any>) => {
19+
const forceLogout = (store: Store<any>) => {
2520
store.dispatch(StoreConfig.auth.logout, true).then(() => {
2621
console.log('the user is forced to logout')
2722
})
@@ -79,10 +74,10 @@ const firebaseAuthListenerPlugin: Plugin = ({ store, app, route, redirect }) =>
7974
})
8075

8176
auth.onAuthStateChanged((firebaseUser: User|null) => {
82-
return Promise.resolve()
77+
return store.dispatch(StoreConfig.loading.saveLoading, true)
8378
.then(async () => {
8479
if (store.state.auth.forceLogout) {
85-
logout(store)
80+
forceLogout(store)
8681
return
8782
}
8883

@@ -110,23 +105,25 @@ const firebaseAuthListenerPlugin: Plugin = ({ store, app, route, redirect }) =>
110105
}
111106
}
112107
})
108+
.finally(() => store.dispatch(StoreConfig.loading.saveLoading, false))
113109
})
114110

115111
auth.onIdTokenChanged((firebaseUser: User|null) => {
116-
console.log('onIdTokenChanged with a user: ', !!firebaseUser)
117-
if (!firebaseUser) {
118-
return
119-
}
120-
updateAuthStore(firebaseUser, store)
112+
store.dispatch(StoreConfig.loading.saveLoading, true)
121113
.then(() => {
122-
reloadUserFromDatabase.next()
123-
})
114+
console.log('onIdTokenChanged with a user: ', !!firebaseUser)
115+
if (!firebaseUser) {
116+
return
117+
}
118+
updateAuthStore(firebaseUser, store)
124119

125-
firebaseUser.getIdToken()
126-
.then((token: string) => {
127-
app.$axios.setToken(token, AppTokenType)
128-
app.$cookies.set(AppCookie.TOKEN, token, cookieOptions)
120+
firebaseUser.getIdToken()
121+
.then((token: string) => {
122+
app.$axios.setToken(token, AppTokenType)
123+
app.$cookies.set(AppCookie.TOKEN, token, cookieOptions)
124+
})
129125
})
126+
.finally(() => store.dispatch(StoreConfig.loading.saveLoading, false))
130127
})
131128
}
132129

src/service/firebase/firestore/user-collection.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ import {
1515
saveModel
1616
} from '~/service/firebase/firestore/collection-base-service'
1717
import { PagingResponse, SearchData } from '~/types'
18+
import { generateNumericByLength } from '~/service/global-service'
1819

19-
export const saveUser = async (user: User): Promise<User> => {
20-
return await saveModel(collection.USER, user)
20+
export const saveUser = (user: User): Promise<User> => {
21+
return saveModel(collection.USER, user)
2122
}
2223

23-
export const getUser = async (id: string): Promise<User> => {
24-
return await getModelById(collection.USER, id)
24+
export const getUser = (id: string): Promise<User> => {
25+
return getModelById(collection.USER, id)
26+
}
27+
28+
export const getAvailableUsername = (username: string, originalUsername ?: string): Promise<string> => {
29+
return getUserByUsername(username).then(async (existingUser) => {
30+
if (!existingUser) {
31+
return username
32+
}
33+
return await getAvailableUsername(`${username}${generateNumericByLength(4)}`, originalUsername || username)
34+
})
2535
}
2636

2737
export const getUserByUsername = async (

src/service/global-service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export const generateUuid = () => {
1313
return uuidv4().replace(dashAllRegex, '')
1414
}
1515

16+
export const generateNumericByLength = (length: number) => {
17+
return Math.random().toString(9).substr(2, length)
18+
}
19+
1620
export const log = (...anyMessages: any[]) => {
1721
console.log(`${moment().format(timestampFormat)} | `, ...anyMessages)
1822
}
@@ -80,3 +84,8 @@ export const getPageRouteWithQuery = (routeType: RouteType, query: string) => {
8084
}
8185
}
8286
}
87+
88+
export const generateUsername = (username: string) => {
89+
const usernamePrefix = username.replace(/@.*$/, '')
90+
return slugify(usernamePrefix).replace(dashAllRegex, '')
91+
}

src/store/auth.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import {
2525
import { getProviderOption, refreshToken, updateProfile } from '~/service/firebase/firebase-service'
2626
import { handleError } from '~/service/error-service'
2727
import { reauthenticateObservable } from '~/service/rx-service'
28-
import { getUser, saveUser } from '~/service/firebase/firestore'
28+
import { getAvailableUsername, getUser, saveUser } from '~/service/firebase/firestore'
2929
import { authClaims } from '~/service/api-service'
3030
import { deleteUserDeviceByToken } from '~/service/firebase/firestore/user-device-collection'
31+
import { generateUsername } from '~/service/global-service'
3132
import UserCredential = firebase.auth.UserCredential
3233
import ActionCodeInfo = firebase.auth.ActionCodeInfo
3334
import Persistence = firebase.auth.Auth.Persistence
@@ -92,15 +93,16 @@ export const actions: ActionTree<AuthState, RootState> = {
9293
.then(async (userCredential: UserCredential) => {
9394
// save user to db
9495
const id = userCredential.user?.uid as string
96+
const username = await getAvailableUsername(generateUsername(userCredential.user.email))
9597
await updateProfile(credentials.name, DefaultProfilePhoto.src)
96-
.then(async () => await authClaims(this.$axios, id))
98+
.then(async () => await authClaims(this.$axios, username))
9799
.then(() => refreshToken())
98100

99101
await saveUser({
100102
id,
101103
name: credentials.name,
102104
email: credentials.email,
103-
username: id,
105+
username,
104106
privacy: PrivacyType.PUBLIC,
105107
followersPrivacy: PrivacyType.PUBLIC,
106108
followingPrivacy: PrivacyType.PUBLIC,
@@ -156,15 +158,16 @@ export const actions: ActionTree<AuthState, RootState> = {
156158
}
157159

158160
const name = userCredential.user?.displayName as string
161+
const username = await getAvailableUsername(generateUsername(name))
159162
const photo = userCredential.user?.photoURL as string
160163

161-
await authClaims(this.$axios, id)
164+
await authClaims(this.$axios, username)
162165
.then(() => refreshToken())
163166

164167
await saveUser({
165168
id,
166169
name,
167-
username: id,
170+
username,
168171
privacy: PrivacyType.PUBLIC,
169172
followersPrivacy: PrivacyType.PUBLIC,
170173
followingPrivacy: PrivacyType.PUBLIC,

src/types/state-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const StoreConfig = {
5757
clearNotificationMessage: 'notification/clearNotificationMessage'
5858
},
5959
loading: {
60-
setLoading: 'loading/setLoading'
60+
setLoading: 'loading/setLoading',
61+
saveLoading: 'loading/saveLoading'
6162
}
6263
}

0 commit comments

Comments
 (0)