Make email configuration optional
This commit is contained in:
parent
bb8eb7ac82
commit
1c36d8c480
@ -4,7 +4,7 @@ struct Configuration {
|
||||
|
||||
let serverPort: Int
|
||||
|
||||
let mail: EMail
|
||||
let mail: EMail?
|
||||
|
||||
/**
|
||||
Use a database file and reduce logging.
|
||||
|
@ -6,6 +6,15 @@ import SwiftSMTP
|
||||
typealias PasswordHash = String
|
||||
typealias SessionToken = String
|
||||
|
||||
private struct MailConfig {
|
||||
|
||||
let mailConfiguration: Configuration.EMail
|
||||
|
||||
let smtp: SMTP
|
||||
|
||||
let mailSender: Mail.User
|
||||
}
|
||||
|
||||
final class SQLiteDatabase {
|
||||
|
||||
/// A mapping between player name and generated access tokens for a session
|
||||
@ -16,20 +25,21 @@ final class SQLiteDatabase {
|
||||
|
||||
private let tables: TableManagement
|
||||
|
||||
private let mailConfiguration: Configuration.EMail
|
||||
private let mail: MailConfig?
|
||||
|
||||
private let smtp: SMTP
|
||||
|
||||
private let mailSender: Mail.User
|
||||
|
||||
init(db: Database, mail: Configuration.EMail) throws {
|
||||
init(db: Database, mail: Configuration.EMail?) throws {
|
||||
self.tables = try TableManagement(db: db)
|
||||
self.smtp = SMTP(
|
||||
hostname: mail.emailHostname,
|
||||
email: mail.email,
|
||||
password: mail.password)
|
||||
self.mailSender = Mail.User(name: "Schafkopf Server", email: mail.email)
|
||||
self.mailConfiguration = mail
|
||||
guard let mail else {
|
||||
self.mail = nil
|
||||
return
|
||||
}
|
||||
let smtp = SMTP(
|
||||
hostname: mail.emailHostname,
|
||||
email: mail.email,
|
||||
password: mail.password)
|
||||
let mailSender = Mail.User(name: "Schafkopf Server", email: mail.email)
|
||||
|
||||
self.mail = .init(mailConfiguration: mail, smtp: smtp, mailSender: mailSender)
|
||||
}
|
||||
|
||||
func registerPlayer(named name: PlayerName, hash: PasswordHash, email: String?, in database: Database) async throws -> SessionToken {
|
||||
@ -81,10 +91,15 @@ final class SQLiteDatabase {
|
||||
}
|
||||
|
||||
private func sendEmail(name: PlayerName, email: String, token: String) {
|
||||
guard let mailData = mail else {
|
||||
print("Recovery email not set up")
|
||||
return
|
||||
}
|
||||
let recipient = Mail.User(name: name, email: email)
|
||||
let mailConfiguration = mailData.mailConfiguration
|
||||
let url = "\(mailConfiguration.serverDomain)/recovery.html?token=\(token)"
|
||||
let mail = Mail(
|
||||
from: mailSender,
|
||||
from: mailData.mailSender,
|
||||
to: [recipient],
|
||||
subject: "Schafkopf Server Password Reset",
|
||||
text:
|
||||
@ -104,7 +119,7 @@ final class SQLiteDatabase {
|
||||
"""
|
||||
)
|
||||
|
||||
smtp.send(mail) { (error) in
|
||||
mailData.smtp.send(mail) { (error) in
|
||||
if let error = error {
|
||||
print("Failed to send recovery email to \(email): \(error)")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user