2018-04-02 20:23:44 +02:00
|
|
|
// Free API to get location from IP: http://freegeoip.net/json/149.11.144.50
|
2018-05-01 16:38:11 +02:00
|
|
|
import randomstring from 'random-string'
|
2018-05-04 19:53:16 +02:00
|
|
|
import {sendEmail} from './post_office'
|
|
|
|
import {domain} from './config'
|
|
|
|
import Repository from './repository'
|
|
|
|
import {User} from "./types";
|
|
|
|
|
|
|
|
export class SessionInvite {
|
|
|
|
|
|
|
|
oneTimeKey: string
|
|
|
|
userId: string
|
|
|
|
|
|
|
|
constructor(user: User) {
|
2018-05-05 19:50:45 +02:00
|
|
|
console.log('RANDOMSTRING', randomstring)
|
2018-05-04 19:53:16 +02:00
|
|
|
this.oneTimeKey = randomstring({
|
2018-04-02 20:23:44 +02:00
|
|
|
length: 24,
|
2018-05-01 16:38:11 +02:00
|
|
|
letters: true,
|
2018-05-04 19:53:16 +02:00
|
|
|
special: false
|
2018-04-02 20:23:44 +02:00
|
|
|
})
|
2018-05-04 19:53:16 +02:00
|
|
|
this.userId = user.id
|
2018-04-02 20:23:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 19:53:16 +02:00
|
|
|
export default class SessionManager {
|
|
|
|
|
|
|
|
repository: Repository
|
|
|
|
|
|
|
|
constructor(repository: Repository){
|
|
|
|
this.repository = repository
|
|
|
|
}
|
|
|
|
|
|
|
|
inviteUser = async (user: User) => {
|
|
|
|
const invite = new SessionInvite(user)
|
|
|
|
try {
|
|
|
|
const persistedInvite = await this.repository.saveSessionInvite(invite)
|
|
|
|
console.log(`Invite persisted for user ${user.username}`, persistedInvite)
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Failed to save invite', invite)
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const sentMail = await sendEmail({
|
|
|
|
to: user.email,
|
|
|
|
from: `signin@${domain}`,
|
|
|
|
text: `invitation for session key: ${invite.oneTimeKey}`,
|
|
|
|
subject: 'Invitation for session'
|
|
|
|
})
|
|
|
|
console.log(`Sent invitation to ${user.email}`)
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Failed to send invitation email', invite)
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|