Skip to content

Commit 81cd271

Browse files
committed
Fix Auth
1 parent f21882d commit 81cd271

6 files changed

Lines changed: 53 additions & 7 deletions

File tree

api/pkg/di/container.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,23 @@ func (container *Container) App() (app *fiber.App) {
8585

8686
app.Use(middlewares.BearerAuth(container.Logger(), container.Tracer(), container.FirebaseAuthClient()))
8787
app.Use(middlewares.APIKeyAuth(container.Logger(), container.Tracer(), container.UserRepository()))
88-
app.Use(middlewares.Authenticated(container.Tracer()))
8988

9089
container.app = app
9190
return app
9291
}
9392

93+
// AuthenticatedMiddleware creates a new instance of middlewares.Authenticated
94+
func (container *Container) AuthenticatedMiddleware() fiber.Handler {
95+
container.logger.Debug("creating middlewares.Authenticated")
96+
return middlewares.Authenticated(container.Tracer())
97+
}
98+
99+
// AuthRouter creates router for authenticated requests
100+
func (container *Container) AuthRouter() fiber.Router {
101+
container.logger.Debug("creating authRouter")
102+
return container.App().Group("v1").Use(container.AuthenticatedMiddleware())
103+
}
104+
94105
// Logger creates a new instance of telemetry.Logger
95106
func (container *Container) Logger() telemetry.Logger {
96107
container.logger.Debug("creating telemetry.Logger")
@@ -442,31 +453,31 @@ func (container *Container) MessageService() (service *services.MessageService)
442453
// RegisterMessageRoutes registers routes for the /messages prefix
443454
func (container *Container) RegisterMessageRoutes() {
444455
container.logger.Debug(fmt.Sprintf("registering %T routes", &handlers.MessageHandler{}))
445-
container.MessageHandler().RegisterRoutes(container.App().Group("v1"))
456+
container.MessageHandler().RegisterRoutes(container.AuthRouter())
446457
}
447458

448459
// RegisterMessageThreadRoutes registers routes for the /message-threads prefix
449460
func (container *Container) RegisterMessageThreadRoutes() {
450461
container.logger.Debug(fmt.Sprintf("registering %T routes", &handlers.MessageThreadHandler{}))
451-
container.MessageThreadHandler().RegisterRoutes(container.App().Group("v1"))
462+
container.MessageThreadHandler().RegisterRoutes(container.AuthRouter())
452463
}
453464

454465
// RegisterHeartbeatRoutes registers routes for the /heartbeats prefix
455466
func (container *Container) RegisterHeartbeatRoutes() {
456467
container.logger.Debug(fmt.Sprintf("registering %T routes", &handlers.HeartbeatHandler{}))
457-
container.HeartbeatHandler().RegisterRoutes(container.App().Group("v1"))
468+
container.HeartbeatHandler().RegisterRoutes(container.AuthRouter())
458469
}
459470

460471
// RegisterPhoneRoutes registers routes for the /phone prefix
461472
func (container *Container) RegisterPhoneRoutes() {
462473
container.logger.Debug(fmt.Sprintf("registering %T routes", &handlers.PhoneHandler{}))
463-
container.PhoneHandler().RegisterRoutes(container.App().Group("v1"))
474+
container.PhoneHandler().RegisterRoutes(container.AuthRouter())
464475
}
465476

466477
// RegisterUserRoutes registers routes for the /users prefix
467478
func (container *Container) RegisterUserRoutes() {
468479
container.logger.Debug(fmt.Sprintf("registering %T routes", &handlers.UserHandler{}))
469-
container.UserHandler().RegisterRoutes(container.App().Group("v1"))
480+
container.UserHandler().RegisterRoutes(container.AuthRouter())
470481
}
471482

472483
// RegisterSwaggerRoutes registers routes for swagger

web/layouts/default.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export default class DefaultLayout extends Vue {
4646
if (this.$store.getters.getOwner) {
4747
promises.push(
4848
this.$store.dispatch('loadThreads'),
49-
this.$store.dispatch('getHeartbeat')
49+
this.$store.dispatch('getHeartbeat'),
50+
this.$store.dispatch('loadPhones', true)
5051
)
5152
}
5253

web/models/phone.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface Phone {
2+
id: string
3+
user_id: string
4+
fcm_token: string
5+
phone_number: string
6+
created_at: string
7+
updated_at: string
8+
}

web/pages/threads/_id.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export default Vue.extend({
156156
},
157157
158158
async mounted() {
159+
await this.$store.dispatch('loadPhones')
159160
await this.$store.dispatch('loadThreads')
160161
await this.$store.dispatch('loadThreadMessages', this.$route.params.id)
161162
this.scrollToElement()

web/pages/threads/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default {
3030
middleware: ['auth'],
3131
mounted() {
3232
this.$store.dispatch('loadThreads')
33+
this.$store.dispatch('loadPhones')
3334
},
3435
}
3536
</script>

web/store/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MessageThread } from '~/models/message-thread'
33
import { Message } from '~/models/message'
44
import { Heartbeat } from '~/models/heartbeat'
55
import axios from '~/plugins/axios'
6+
import { Phone } from '~/models/phone'
67

78
const defaultNotificationTimeout = 3000
89

@@ -27,6 +28,7 @@ export type User = {
2728
export type State = {
2829
owner: string
2930
user: User | null
31+
phones: Array<Phone>
3032
threads: Array<MessageThread>
3133
threadId: string | null
3234
heartbeat: null | Heartbeat
@@ -41,6 +43,7 @@ export const state = (): State => ({
4143
heartbeat: null,
4244
pooling: false,
4345
threadMessages: [],
46+
phones: [],
4447
owner: '+37259139660',
4548
user: null,
4649
notification: {
@@ -84,6 +87,10 @@ export const getters = {
8487
return state.owner
8588
},
8689

90+
getPhones(state: State): Array<Phone> {
91+
return state.phones
92+
},
93+
8794
hasThread(state: State): boolean {
8895
return state.threadId != null
8996
},
@@ -144,6 +151,15 @@ export const mutations = {
144151
disableNotification(state: State) {
145152
state.notification.active = false
146153
},
154+
155+
setPhones(state: State, payload: Array<Phone>) {
156+
state.phones = payload
157+
158+
const owner = payload.find((x) => x.phone_number === state.owner)
159+
if (!owner && state.phones.length > 0) {
160+
state.owner = state.phones[0].phone_number
161+
}
162+
},
147163
}
148164

149165
export type SendMessageRequest = {
@@ -162,6 +178,14 @@ export const actions = {
162178
context.commit('setThreads', response.data.data)
163179
},
164180

181+
async loadPhones(context: ActionContext<State, State>, force: boolean) {
182+
if (context.getters.getPhones.length > 0 && !force) {
183+
return
184+
}
185+
const response = await axios.get('/v1/phones')
186+
context.commit('setPhones', response.data.data)
187+
},
188+
165189
async getHeartbeat(context: ActionContext<State, State>) {
166190
const response = await axios.get('/v1/heartbeats', {
167191
params: {

0 commit comments

Comments
 (0)