Skip to content

Commit

Permalink
Rewrite UserSession for proto files.
Browse files Browse the repository at this point in the history
  • Loading branch information
MykytaPimonovTD committed Nov 14, 2024
1 parent 65817df commit 5e38eba
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 107 deletions.
107 changes: 0 additions & 107 deletions client/src/main/kotlin/io/spine/examples/pingh/client/UserSession.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2024, TeamDev. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.examples.pingh.client

import io.spine.examples.pingh.github.Username
import io.spine.examples.pingh.github.of
import kotlin.reflect.KClass

/**
* The username to which the current session belongs.
*/
internal val UserSession.username: Username
get() = this.id?.username ?: Guest.username

/**
* Returns `true` if the session is authenticated, and `false` if it is a guest session.
*/
internal fun UserSession.isAuthenticated() = this != Guest.session

/**
* Saves the user session data to a file in the user's data directory.
*/
internal fun UserSession.save() {
FileStorage.save(FileLocation.Session, this)
}

/**
* Loads the user session data from a file in the user's data directory.
*
* Returns a guest session if the file is empty.
*/
@Suppress("UnusedReceiverParameter" /* Associated with the class but doesn't use its data. */)
internal fun KClass<UserSession>.loadOrDefault(): UserSession =
FileStorage.loadOrDefault(FileLocation.Session) { Guest.session }

internal object Guest {
internal val session = UserSession.getDefaultInstance()!!
internal val username = Username::class.of("unauthenticated-guest")
}

0 comments on commit 5e38eba

Please sign in to comment.