adding basic register functionality

This commit is contained in:
andy 2022-08-10 23:18:22 +01:00
parent 82197167e4
commit 7477ff0a75
3 changed files with 129 additions and 14 deletions

View File

@ -13,6 +13,7 @@ import SwiftyJSON
public enum AuthApi { public enum AuthApi {
case token(username: String, password: String, expiry: Int) case token(username: String, password: String, expiry: Int)
case register(username: String, password: String, password2: String)
} }
extension AuthApi: ApiRequest { extension AuthApi: ApiRequest {
@ -24,6 +25,8 @@ extension AuthApi: ApiRequest {
switch self { switch self {
case .token: case .token:
return "auth/token" return "auth/token"
case .register:
return "auth/register"
} }
} }
@ -32,6 +35,8 @@ extension AuthApi: ApiRequest {
switch self { switch self {
case .token: case .token:
return .post return .post
case .register:
return .post
} }
} }
@ -39,6 +44,8 @@ extension AuthApi: ApiRequest {
switch self { switch self {
case .token(let username, let password, let expiry): case .token(let username, let password, let expiry):
return JSON(["username": username, "password": password, "expiry": expiry]) return JSON(["username": username, "password": password, "expiry": expiry])
case .register(let username, let password, let password2):
return JSON(["username": username, "password": password, "password_again": password2])
} }
} }
@ -46,6 +53,8 @@ extension AuthApi: ApiRequest {
switch self { switch self {
case .token: case .token:
return JSONParameterEncoder.default return JSONParameterEncoder.default
case.register:
return JSONParameterEncoder.default
} }
} }

View File

@ -27,5 +27,6 @@ struct Router: View {
struct Router_Previews: PreviewProvider { struct Router_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
Router() Router()
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false))
} }
} }

View File

@ -11,16 +11,25 @@ import ToastUI
import KeychainAccess import KeychainAccess
import SwiftyJSON import SwiftyJSON
enum ScreenMode {
case None
case Login
case Register
}
struct LoginScreen: View { struct LoginScreen: View {
@EnvironmentObject var liveUser: LiveUser @EnvironmentObject var liveUser: LiveUser
@State private var username: String = "" @State private var username: String = ""
@State private var password: String = "" @State private var password: String = ""
@State private var password2: String = ""
@State private var showingToast = false @State private var showingToast = false
@State private var toastText = "" @State private var toastText = ""
@State public var screenMode = ScreenMode.None
var body: some View { var body: some View {
VStack { VStack {
Image("MusicToolsLogo") Image("MusicToolsLogo")
@ -29,17 +38,50 @@ struct LoginScreen: View {
.cornerRadius(18) .cornerRadius(18)
.shadow(radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/) .shadow(radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
.padding(.bottom, 20) .padding(.bottom, 20)
Text("Sarsoo's Mixonomer") Text("Mixonomer")
.font(.largeTitle) .font(.largeTitle)
TextField("Username", text: $username) .padding(.bottom, 20)
SecureField("Password", text: $password)
HStack { if screenMode != .None {
TextField("Username", text: $username)
.textFieldStyle(.roundedBorder)
SecureField("Password", text: $password)
.textFieldStyle(.roundedBorder)
}
if screenMode == .Register {
SecureField("Password Again", text: $password2)
.textFieldStyle(.roundedBorder)
.padding(.bottom, 20)
}
if screenMode == .None {
HStack {
Button(action: {
screenMode = .Login
}) {
Text("Log In")
}
.buttonStyle(.bordered)
.padding(.trailing, 20.0)
Button(action: {
screenMode = .Register
}) {
Text("Register")
}.buttonStyle(.bordered)
}
}
if screenMode == .Login {
Button(action: { Button(action: {
let keychain = Keychain(service: "xyz.sarsoo.music.login") let keychain = Keychain(service: "xyz.sarsoo.music.login")
keychain["username"] = username keychain["username"] = username
// keychain["password"] = password
let api = AuthApi.token(username: username, password: password, expiry: 604800) let api = AuthApi.token(username: username, password: password, expiry: 604800)
RequestBuilder.buildRequest(apiRequest: api) RequestBuilder.buildRequest(apiRequest: api)
@ -65,7 +107,6 @@ struct LoginScreen: View {
case _: case _:
keychain["username"] = nil keychain["username"] = nil
keychain["password"] = nil
keychain["jwt"] = nil keychain["jwt"] = nil
toastText = "Login Failed" toastText = "Login Failed"
@ -75,16 +116,73 @@ struct LoginScreen: View {
}) { }) {
Text("Log In") Text("Log In")
} }
.padding(.trailing, 20.0) .buttonStyle(.bordered)
Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) { .padding(.top, 20)
}
if screenMode == .Register {
Button(action: {
let keychain = Keychain(service: "xyz.sarsoo.music.login")
keychain["username"] = username
let api = AuthApi.register(username: username, password: password, password2: password2)
RequestBuilder.buildRequest(apiRequest: api)
.validate()
.responseJSON { response in
switch response.response?.statusCode {
case 200, 201:
let token_api = AuthApi.token(username: username, password: password, expiry: 604800)
RequestBuilder.buildRequest(apiRequest: token_api)
.validate()
.responseJSON { response in
switch response.response?.statusCode {
case 200, 201:
guard let data = response.data else {
fatalError("error getting token")
}
guard let json = try? JSON(data: data) else {
fatalError("error parsing reponse")
}
let token = json["token"].stringValue
keychain["jwt"] = token
self.liveUser.loggedIn = true
case _:
keychain["username"] = nil
keychain["jwt"] = nil
toastText = "Token Generation Failed"
showingToast = true
}
}
// TODO: add handling for 400, password dont match
// TODO: add handling for 409, conflict
case _:
keychain["username"] = nil
keychain["jwt"] = nil
toastText = "Register Failed"
showingToast = true
}
}
}) {
Text("Register") Text("Register")
} }
.buttonStyle(.bordered)
} }
.toast(isPresented: $showingToast, dismissAfter: 3.0){
ToastView(toastText)
.toastViewStyle(.failure)
}
.toastDimmedBackground(false)
} }
.padding() .padding()
} }
@ -92,6 +190,13 @@ struct LoginScreen: View {
struct LoginScreen_Previews: PreviewProvider { struct LoginScreen_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
LoginScreen() Group{
LoginScreen(screenMode: .None)
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false))
LoginScreen(screenMode: .Login)
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false))
LoginScreen(screenMode: .Register)
.environmentObject(LiveUser(playlists: [], tags: [], username: "user", loggedIn: false))
}
} }
} }